|
| 1 | +/** |
| 2 | + * Globalize Runtime v1.4.0-alpha.3 |
| 3 | + * |
| 4 | + * http://github.com/jquery/globalize |
| 5 | + * |
| 6 | + * Copyright jQuery Foundation and other contributors |
| 7 | + * Released under the MIT license |
| 8 | + * http://jquery.org/license |
| 9 | + * |
| 10 | + * Date: 2018-06-22T12:51Z |
| 11 | + */ |
| 12 | +/*! |
| 13 | + * Globalize Runtime v1.4.0-alpha.3 2018-06-22T12:51Z Released under the MIT license |
| 14 | + * http://git.io/TrdQbw |
| 15 | + */ |
| 16 | +(function( root, factory ) { |
| 17 | + |
| 18 | + "use strict"; |
| 19 | + |
| 20 | + // UMD returnExports |
| 21 | + if ( typeof define === "function" && define.amd ) { |
| 22 | + |
| 23 | + // AMD |
| 24 | + define( factory ); |
| 25 | + } else if ( typeof exports === "object" ) { |
| 26 | + |
| 27 | + // Node, CommonJS |
| 28 | + module.exports = factory(); |
| 29 | + } else { |
| 30 | + |
| 31 | + // Globalize |
| 32 | + root.Globalize = factory(); |
| 33 | + } |
| 34 | +}( this, function() { |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * A toString method that outputs meaningful values for objects or arrays and |
| 41 | + * still performs as fast as a plain string in case variable is string, or as |
| 42 | + * fast as `"" + number` in case variable is a number. |
| 43 | + * Ref: http://jsperf.com/my-stringify |
| 44 | + */ |
| 45 | +var toString = function( variable ) { |
| 46 | + return typeof variable === "string" ? variable : ( typeof variable === "number" ? "" + |
| 47 | + variable : JSON.stringify( variable ) ); |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +/** |
| 54 | + * formatMessage( message, data ) |
| 55 | + * |
| 56 | + * @message [String] A message with optional {vars} to be replaced. |
| 57 | + * |
| 58 | + * @data [Array or JSON] Object with replacing-variables content. |
| 59 | + * |
| 60 | + * Return the formatted message. For example: |
| 61 | + * |
| 62 | + * - formatMessage( "{0} second", [ 1 ] ); // 1 second |
| 63 | + * |
| 64 | + * - formatMessage( "{0}/{1}", ["m", "s"] ); // m/s |
| 65 | + * |
| 66 | + * - formatMessage( "{name} <{email}>", { |
| 67 | + * name: "Foo", |
| 68 | + * email: "bar@baz.qux" |
| 69 | + * }); // Foo <bar@baz.qux> |
| 70 | + */ |
| 71 | +var formatMessage = function( message, data ) { |
| 72 | + |
| 73 | + // Replace {attribute}'s |
| 74 | + message = message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( name ) { |
| 75 | + name = name.replace( /^{([^}]*)}$/, "$1" ); |
| 76 | + return toString( data[ name ] ); |
| 77 | + }); |
| 78 | + |
| 79 | + return message; |
| 80 | +}; |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +var objectExtend = function() { |
| 86 | + var destination = arguments[ 0 ], |
| 87 | + sources = [].slice.call( arguments, 1 ); |
| 88 | + |
| 89 | + sources.forEach(function( source ) { |
| 90 | + var prop; |
| 91 | + for ( prop in source ) { |
| 92 | + destination[ prop ] = source[ prop ]; |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + return destination; |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +var createError = function( code, message, attributes ) { |
| 103 | + var error; |
| 104 | + |
| 105 | + message = code + ( message ? ": " + formatMessage( message, attributes ) : "" ); |
| 106 | + error = new Error( message ); |
| 107 | + error.code = code; |
| 108 | + |
| 109 | + objectExtend( error, attributes ); |
| 110 | + |
| 111 | + return error; |
| 112 | +}; |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +// Based on http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery |
| 118 | +var stringHash = function( str ) { |
| 119 | + return [].reduce.call( str, function( hash, i ) { |
| 120 | + var chr = i.charCodeAt( 0 ); |
| 121 | + hash = ( ( hash << 5 ) - hash ) + chr; |
| 122 | + return hash | 0; |
| 123 | + }, 0 ); |
| 124 | +}; |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +var runtimeKey = function( fnName, locale, args, argsStr ) { |
| 130 | + var hash; |
| 131 | + argsStr = argsStr || JSON.stringify( args ); |
| 132 | + hash = stringHash( fnName + locale + argsStr ); |
| 133 | + return hash > 0 ? "a" + hash : "b" + Math.abs( hash ); |
| 134 | +}; |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +var validate = function( code, message, check, attributes ) { |
| 140 | + if ( !check ) { |
| 141 | + throw createError( code, message, attributes ); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +var validateParameterPresence = function( value, name ) { |
| 149 | + validate( "E_MISSING_PARAMETER", "Missing required parameter `{name}`.", |
| 150 | + value !== undefined, { name: name }); |
| 151 | +}; |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +var validateParameterType = function( value, name, check, expected ) { |
| 157 | + validate( |
| 158 | + "E_INVALID_PAR_TYPE", |
| 159 | + "Invalid `{name}` parameter ({value}). {expected} expected.", |
| 160 | + check, |
| 161 | + { |
| 162 | + expected: expected, |
| 163 | + name: name, |
| 164 | + value: value |
| 165 | + } |
| 166 | + ); |
| 167 | +}; |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +var validateParameterTypeString = function( value, name ) { |
| 173 | + validateParameterType( |
| 174 | + value, |
| 175 | + name, |
| 176 | + value === undefined || typeof value === "string", |
| 177 | + "a string" |
| 178 | + ); |
| 179 | +}; |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions |
| 185 | +var regexpEscape = function( string ) { |
| 186 | + return string.replace( /([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1" ); |
| 187 | +}; |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +var stringPad = function( str, count, right ) { |
| 193 | + var length; |
| 194 | + if ( typeof str !== "string" ) { |
| 195 | + str = String( str ); |
| 196 | + } |
| 197 | + for ( length = str.length; length < count; length += 1 ) { |
| 198 | + str = ( right ? ( str + "0" ) : ( "0" + str ) ); |
| 199 | + } |
| 200 | + return str; |
| 201 | +}; |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +function Globalize( locale ) { |
| 207 | + if ( !( this instanceof Globalize ) ) { |
| 208 | + return new Globalize( locale ); |
| 209 | + } |
| 210 | + |
| 211 | + validateParameterPresence( locale, "locale" ); |
| 212 | + validateParameterTypeString( locale, "locale" ); |
| 213 | + |
| 214 | + this._locale = locale; |
| 215 | +} |
| 216 | + |
| 217 | +Globalize.locale = function( locale ) { |
| 218 | + validateParameterTypeString( locale, "locale" ); |
| 219 | + |
| 220 | + if ( arguments.length ) { |
| 221 | + this._locale = locale; |
| 222 | + } |
| 223 | + return this._locale; |
| 224 | +}; |
| 225 | + |
| 226 | +Globalize._createError = createError; |
| 227 | +Globalize._formatMessage = formatMessage; |
| 228 | +Globalize._regexpEscape = regexpEscape; |
| 229 | +Globalize._runtimeKey = runtimeKey; |
| 230 | +Globalize._stringPad = stringPad; |
| 231 | +Globalize._validateParameterPresence = validateParameterPresence; |
| 232 | +Globalize._validateParameterTypeString = validateParameterTypeString; |
| 233 | +Globalize._validateParameterType = validateParameterType; |
| 234 | + |
| 235 | +return Globalize; |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | +})); |
0 commit comments