|
| 1 | +/** |
| 2 | + * Globalize Runtime v1.7.0 |
| 3 | + * |
| 4 | + * https://github.com/globalizejs/globalize |
| 5 | + * |
| 6 | + * Copyright OpenJS Foundation and other contributors |
| 7 | + * Released under the MIT license |
| 8 | + * https://jquery.org/license |
| 9 | + * |
| 10 | + * Date: 2021-08-02T11:53Z |
| 11 | + */ |
| 12 | +/*! |
| 13 | + * Globalize Runtime v1.7.0 2021-08-02T11:53Z 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 | +/** |
| 118 | + * Pushes part to parts array, concat two consecutive parts of the same type. |
| 119 | + */ |
| 120 | +var partsPush = function( parts, type, value ) { |
| 121 | + |
| 122 | + // Concat two consecutive parts of same type |
| 123 | + if ( parts.length && parts[ parts.length - 1 ].type === type ) { |
| 124 | + parts[ parts.length - 1 ].value += value; |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + parts.push( { type: type, value: value } ); |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +/** |
| 135 | + * formatMessage( message, data ) |
| 136 | + * |
| 137 | + * @message [String] A message with optional {vars} to be replaced. |
| 138 | + * |
| 139 | + * @data [Array or JSON] Object with replacing-variables content. |
| 140 | + * |
| 141 | + * Return the formatted message. For example: |
| 142 | + * |
| 143 | + * - formatMessage( "{0} second", [ 1 ] ); |
| 144 | + * > [{type: "variable", value: "1", name: "0"}, {type: "literal", value: " second"}] |
| 145 | + * |
| 146 | + * - formatMessage( "{0}/{1}", ["m", "s"] ); |
| 147 | + * > [ |
| 148 | + * { type: "variable", value: "m", name: "0" }, |
| 149 | + * { type: "literal", value: " /" }, |
| 150 | + * { type: "variable", value: "s", name: "1" } |
| 151 | + * ] |
| 152 | + */ |
| 153 | +var formatMessageToParts = function( message, data ) { |
| 154 | + |
| 155 | + var lastOffset = 0, |
| 156 | + parts = []; |
| 157 | + |
| 158 | + // Create parts. |
| 159 | + message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( nameIncludingBrackets, offset ) { |
| 160 | + var name = nameIncludingBrackets.slice( 1, -1 ); |
| 161 | + partsPush( parts, "literal", message.slice( lastOffset, offset )); |
| 162 | + partsPush( parts, "variable", data[ name ] ); |
| 163 | + parts[ parts.length - 1 ].name = name; |
| 164 | + lastOffset += offset + nameIncludingBrackets.length; |
| 165 | + }); |
| 166 | + |
| 167 | + // Skip empty ones such as `{ type: 'literal', value: '' }`. |
| 168 | + return parts.filter(function( part ) { |
| 169 | + return part.value !== ""; |
| 170 | + }); |
| 171 | +}; |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | +/** |
| 177 | + * Returns joined parts values. |
| 178 | + */ |
| 179 | +var partsJoin = function( parts ) { |
| 180 | + return parts.map( function( part ) { |
| 181 | + return part.value; |
| 182 | + }).join( "" ); |
| 183 | +}; |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | +var runtimeStringify = function( args ) { |
| 189 | + return JSON.stringify( args, function( _key, value ) { |
| 190 | + if ( value && value.runtimeKey ) { |
| 191 | + return value.runtimeKey; |
| 192 | + } |
| 193 | + return value; |
| 194 | + } ); |
| 195 | +}; |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +// Based on http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery |
| 201 | +var stringHash = function( str ) { |
| 202 | + return [].reduce.call( str, function( hash, i ) { |
| 203 | + var chr = i.charCodeAt( 0 ); |
| 204 | + hash = ( ( hash << 5 ) - hash ) + chr; |
| 205 | + return hash | 0; |
| 206 | + }, 0 ); |
| 207 | +}; |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +var runtimeKey = function( fnName, locale, args, argsStr ) { |
| 213 | + var hash; |
| 214 | + argsStr = argsStr || runtimeStringify( args ); |
| 215 | + hash = stringHash( fnName + locale + argsStr ); |
| 216 | + return hash > 0 ? "a" + hash : "b" + Math.abs( hash ); |
| 217 | +}; |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +var validate = function( code, message, check, attributes ) { |
| 223 | + if ( !check ) { |
| 224 | + throw createError( code, message, attributes ); |
| 225 | + } |
| 226 | +}; |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +var validateParameterPresence = function( value, name ) { |
| 232 | + validate( "E_MISSING_PARAMETER", "Missing required parameter `{name}`.", |
| 233 | + value !== undefined, { name: name }); |
| 234 | +}; |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | +var validateParameterType = function( value, name, check, expected ) { |
| 240 | + validate( |
| 241 | + "E_INVALID_PAR_TYPE", |
| 242 | + "Invalid `{name}` parameter ({value}). {expected} expected.", |
| 243 | + check, |
| 244 | + { |
| 245 | + expected: expected, |
| 246 | + name: name, |
| 247 | + value: value |
| 248 | + } |
| 249 | + ); |
| 250 | +}; |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | +var validateParameterTypeString = function( value, name ) { |
| 256 | + validateParameterType( |
| 257 | + value, |
| 258 | + name, |
| 259 | + value === undefined || typeof value === "string", |
| 260 | + "a string" |
| 261 | + ); |
| 262 | +}; |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | +// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions |
| 268 | +var regexpEscape = function( string ) { |
| 269 | + return string.replace( /([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1" ); |
| 270 | +}; |
| 271 | + |
| 272 | + |
| 273 | + |
| 274 | + |
| 275 | +var stringPad = function( str, count, right ) { |
| 276 | + var length; |
| 277 | + if ( typeof str !== "string" ) { |
| 278 | + str = String( str ); |
| 279 | + } |
| 280 | + for ( length = str.length; length < count; length += 1 ) { |
| 281 | + str = ( right ? ( str + "0" ) : ( "0" + str ) ); |
| 282 | + } |
| 283 | + return str; |
| 284 | +}; |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | +function Globalize( locale ) { |
| 290 | + if ( !( this instanceof Globalize ) ) { |
| 291 | + return new Globalize( locale ); |
| 292 | + } |
| 293 | + |
| 294 | + validateParameterPresence( locale, "locale" ); |
| 295 | + validateParameterTypeString( locale, "locale" ); |
| 296 | + |
| 297 | + this._locale = locale; |
| 298 | +} |
| 299 | + |
| 300 | +Globalize.locale = function( locale ) { |
| 301 | + validateParameterTypeString( locale, "locale" ); |
| 302 | + |
| 303 | + if ( arguments.length ) { |
| 304 | + this._locale = locale; |
| 305 | + } |
| 306 | + return this._locale; |
| 307 | +}; |
| 308 | + |
| 309 | +Globalize._createError = createError; |
| 310 | +Globalize._formatMessage = formatMessage; |
| 311 | +Globalize._formatMessageToParts = formatMessageToParts; |
| 312 | +Globalize._partsJoin = partsJoin; |
| 313 | +Globalize._partsPush = partsPush; |
| 314 | +Globalize._regexpEscape = regexpEscape; |
| 315 | +Globalize._runtimeKey = runtimeKey; |
| 316 | +Globalize._stringPad = stringPad; |
| 317 | +Globalize._validateParameterPresence = validateParameterPresence; |
| 318 | +Globalize._validateParameterTypeString = validateParameterTypeString; |
| 319 | +Globalize._validateParameterType = validateParameterType; |
| 320 | + |
| 321 | +return Globalize; |
| 322 | + |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | +})); |
0 commit comments