diff --git a/.gitignore b/.gitignore index a2cf752..6bc4134 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,2 @@ -lib-cov -*.seed -*.log -*.csv -*.dat -*.out -*.pid -*.gz - -pids -logs -results - -npm-debug.log node_modules -v2 +.cache diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..b4f8e6a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "singleQuote": true, + "arrowParens": "avoid" +} diff --git a/index.js b/index.js index 16dffe1..547b70c 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict'; -var extend = require('extend'); -var sprintf = require("sprintf-js").sprintf; +var extend = require('extend'); +var sprintf = require('sprintf-js').sprintf; var strftime = require('./strftime'); @@ -12,11 +12,17 @@ function isDate(val) { } function isString(val) { - return typeof val === 'string' || Object.prototype.toString.call(val) === '[object String]'; + return ( + typeof val === 'string' || + Object.prototype.toString.call(val) === '[object String]' + ); } function isFunction(val) { - return typeof val === 'function' || Object.prototype.toString.call(val) === '[object Function]'; + return ( + typeof val === 'function' || + Object.prototype.toString.call(val) === '[object Function]' + ); } function isPlainObject(val) { @@ -36,7 +42,7 @@ function hasOwnProp(obj, key) { } function getEntry(translations, keys) { - return keys.reduce(function(result, key) { + return keys.reduce(function (result, key) { if (isPlainObject(result) && hasOwnProp(result, key)) { return result[key]; } else { @@ -48,7 +54,7 @@ function getEntry(translations, keys) { class Counterpart extends EventTarget { constructor() { super(); - + this._registry = { locale: 'en', interpolate: true, @@ -59,14 +65,18 @@ class Counterpart extends EventTarget { normalizedKeys: {}, separator: '.', keepTrailingDot: false, - keyTransformer: function(key) { return key; }, - generateMissingEntry: function(key) { return 'missing translation: ' + key; } + keyTransformer: function (key) { + return key; + }, + generateMissingEntry: function (key) { + return 'missing translation: ' + key; + }, }; - + this.registerTranslations('en', require('./locales/en')); } - - // EventTarget does not (yet) have a native way to retrieve attached listeners. + + // EventTarget does not (yet) have a native way to retrieve attached listeners. // See https://github.com/whatwg/dom/issues/412 #events = {}; @@ -74,18 +84,23 @@ class Counterpart extends EventTarget { _addEventListener = (type, listener, options) => { EventTarget.prototype.addEventListener.call(this, type, listener, options); - (this.#events[type] ||= []).push(listener); + (this.#events[type] ||= []).push(listener); return this; }; _removeEventListener = (type, listener, options) => { - EventTarget.prototype.removeEventListener.call(this, type, listener, options); - + EventTarget.prototype.removeEventListener.call( + this, + type, + listener, + options, + ); + let list = this.#events[type]; - + if (!list) return this; - + let position = -1; for (let i = list.length - 1; i >= 0; i--) { @@ -99,137 +114,142 @@ class Counterpart extends EventTarget { if (position === 0) { list.shift(); - } - else { + } else { list.splice(list, position); } return this; }; - _listenerCount = (type) => { + _listenerCount = type => { return this.#events[type] ? this.#events[type].length : 0; - } + }; } -Counterpart.prototype.getLocale = function() { +Counterpart.prototype.getLocale = function () { return this._registry.locale; }; -Counterpart.prototype.setLocale = function(value) { +Counterpart.prototype.setLocale = function (value) { var previous = this._registry.locale; if (previous != value) { this._registry.locale = value; - this.dispatchEvent(new CustomEvent('localechange', { detail: { locale: value, previous }})); + this.dispatchEvent( + new CustomEvent('localechange', { detail: { locale: value, previous } }), + ); } return previous; }; -Counterpart.prototype.getFallbackLocale = function() { +Counterpart.prototype.getFallbackLocale = function () { return this._registry.fallbackLocales; }; -Counterpart.prototype.setFallbackLocale = function(value) { +Counterpart.prototype.setFallbackLocale = function (value) { var previous = this._registry.fallbackLocales; this._registry.fallbackLocales = [].concat(value || []); return previous; }; -Counterpart.prototype.getAvailableLocales = function() { - return this._registry.availableLocales || Object.keys(this._registry.translations); +Counterpart.prototype.getAvailableLocales = function () { + return ( + this._registry.availableLocales || Object.keys(this._registry.translations) + ); }; -Counterpart.prototype.setAvailableLocales = function(value) { +Counterpart.prototype.setAvailableLocales = function (value) { var previous = this.getAvailableLocales(); this._registry.availableLocales = value; return previous; }; -Counterpart.prototype.getSeparator = function() { +Counterpart.prototype.getSeparator = function () { return this._registry.separator; }; -Counterpart.prototype.setSeparator = function(value) { +Counterpart.prototype.setSeparator = function (value) { var previous = this._registry.separator; this._registry.separator = value; return previous; }; -Counterpart.prototype.setInterpolate = function(value) { +Counterpart.prototype.setInterpolate = function (value) { var previous = this._registry.interpolate; this._registry.interpolate = value; return previous; }; -Counterpart.prototype.getInterpolate = function() { +Counterpart.prototype.getInterpolate = function () { return this._registry.interpolate; }; -Counterpart.prototype.setKeyTransformer = function(value) { +Counterpart.prototype.setKeyTransformer = function (value) { var previous = this._registry.keyTransformer; this._registry.keyTransformer = value; return previous; }; -Counterpart.prototype.getKeyTransformer = function() { +Counterpart.prototype.getKeyTransformer = function () { return this._registry.keyTransformer; }; -Counterpart.prototype.setMissingEntryGenerator = function(value) { +Counterpart.prototype.setMissingEntryGenerator = function (value) { var previous = this._registry.generateMissingEntry; this._registry.generateMissingEntry = value; return previous; }; -Counterpart.prototype.getMissingEntryGenerator = function() { +Counterpart.prototype.getMissingEntryGenerator = function () { return this._registry.generateMissingEntry; }; -Counterpart.prototype.registerTranslations = function(locale, data) { +Counterpart.prototype.registerTranslations = function (locale, data) { var translations = {}; translations[locale] = data; extend(true, this._registry.translations, translations); return translations; }; -Counterpart.prototype.registerInterpolations = function(data) { +Counterpart.prototype.registerInterpolations = function (data) { return extend(true, this._registry.interpolations, data); }; Counterpart.prototype.onLocaleChange = -Counterpart.prototype.addLocaleChangeListener = function(callback) { - this._addEventListener('localechange', callback); -}; + Counterpart.prototype.addLocaleChangeListener = function (callback) { + this._addEventListener('localechange', callback); + }; Counterpart.prototype.offLocaleChange = -Counterpart.prototype.removeLocaleChangeListener = function(callback) { - this._removeEventListener('localechange', callback); -}; + Counterpart.prototype.removeLocaleChangeListener = function (callback) { + this._removeEventListener('localechange', callback); + }; Counterpart.prototype.onTranslationNotFound = -Counterpart.prototype.addTranslationNotFoundListener = function(callback) { - this._addEventListener('translationnotfound', callback); -}; + Counterpart.prototype.addTranslationNotFoundListener = function (callback) { + this._addEventListener('translationnotfound', callback); + }; Counterpart.prototype.offTranslationNotFound = -Counterpart.prototype.removeTranslationNotFoundListener = function(callback) { - this._removeEventListener('translationnotfound', callback); -}; + Counterpart.prototype.removeTranslationNotFoundListener = function ( + callback, + ) { + this._removeEventListener('translationnotfound', callback); + }; -Counterpart.prototype.onError = -Counterpart.prototype.addErrorListener = function(callback) { - this._addEventListener('error', callback); -}; +Counterpart.prototype.onError = Counterpart.prototype.addErrorListener = + function (callback) { + this._addEventListener('error', callback); + }; -Counterpart.prototype.offError = -Counterpart.prototype.removeErrorListener = function(callback) { - this._removeEventListener('error', callback); -}; +Counterpart.prototype.offError = Counterpart.prototype.removeErrorListener = + function (callback) { + this._removeEventListener('error', callback); + }; -Counterpart.prototype.translate = function(key, options) { - if (!Array.isArray(key) && !isString(key) || !key.length) { +Counterpart.prototype.translate = function (key, options) { + if ((!Array.isArray(key) && !isString(key)) || !key.length) { throw new Error('invalid argument: key'); } @@ -250,7 +270,9 @@ Counterpart.prototype.translate = function(key, options) { var separator = options.separator || this._registry.separator; delete options.separator; - var fallbackLocales = [].concat(options.fallbackLocale || this._registry.fallbackLocales); + var fallbackLocales = [].concat( + options.fallbackLocale || this._registry.fallbackLocales, + ); delete options.fallbackLocale; var keys = this._normalizeKeys(locale, scope, key, separator); @@ -258,17 +280,30 @@ Counterpart.prototype.translate = function(key, options) { var entry = getEntry(this._registry.translations, keys); if (entry === null) { - this.dispatchEvent(new CustomEvent('translationnotfound', { detail: { locale, key, fallback: options.fallback, scope }})); + this.dispatchEvent( + new CustomEvent('translationnotfound', { + detail: { locale, key, fallback: options.fallback, scope }, + }), + ); if (options.fallback) { entry = this._fallback(locale, scope, key, options.fallback, options); } } - if (entry === null && fallbackLocales.length > 0 && fallbackLocales.indexOf(locale) === -1) { + if ( + entry === null && + fallbackLocales.length > 0 && + fallbackLocales.indexOf(locale) === -1 + ) { for (var i = 0, ii = fallbackLocales.length; i < ii; i++) { var fallbackLocale = fallbackLocales[i]; - var fallbackKeys = this._normalizeKeys(fallbackLocale, scope, key, separator); + var fallbackKeys = this._normalizeKeys( + fallbackLocale, + scope, + key, + separator, + ); entry = getEntry(this._registry.translations, fallbackKeys); if (entry) { @@ -291,30 +326,37 @@ Counterpart.prototype.translate = function(key, options) { return entry; }; -Counterpart.prototype.localize = function(object, options) { +Counterpart.prototype.localize = function (object, options) { if (!isDate(object)) { throw new Error('invalid argument: object must be a date'); } options = extend(true, {}, options); - var locale = options.locale || this._registry.locale; - var scope = options.scope || translationScope; - var type = options.type || 'datetime'; - var format = options.format || 'default'; + var locale = options.locale || this._registry.locale; + var scope = options.scope || translationScope; + var type = options.type || 'datetime'; + var format = options.format || 'default'; options = { locale: locale, scope: scope, interpolate: false }; - format = this.translate(['formats', type, format], extend(true, {}, options)); + format = this.translate(['formats', type, format], extend(true, {}, options)); return strftime(object, format, this.translate('names', options)); }; -Counterpart.prototype._pluralize = function(locale, entry, count) { - if (typeof entry !== 'object' || entry === null || typeof count !== 'number') { +Counterpart.prototype._pluralize = function (locale, entry, count) { + if ( + typeof entry !== 'object' || + entry === null || + typeof count !== 'number' + ) { return entry; } - var pluralizeFunc = this.translate('pluralize', { locale: locale, scope: translationScope }); + var pluralizeFunc = this.translate('pluralize', { + locale: locale, + scope: translationScope, + }); if (Object.prototype.toString.call(pluralizeFunc) !== '[object Function]') { return pluralizeFunc; @@ -323,7 +365,7 @@ Counterpart.prototype._pluralize = function(locale, entry, count) { return pluralizeFunc(entry, count); }; -Counterpart.prototype.withLocale = function(locale, callback, context) { +Counterpart.prototype.withLocale = function (locale, callback, context) { var previous = this._registry.locale; this._registry.locale = locale; var result = callback.call(context); @@ -331,7 +373,7 @@ Counterpart.prototype.withLocale = function(locale, callback, context) { return result; }; -Counterpart.prototype.withScope = function(scope, callback, context) { +Counterpart.prototype.withScope = function (scope, callback, context) { var previous = this._registry.scope; this._registry.scope = scope; var result = callback.call(context); @@ -339,14 +381,19 @@ Counterpart.prototype.withScope = function(scope, callback, context) { return result; }; -Counterpart.prototype.withSeparator = function(separator, callback, context) { +Counterpart.prototype.withSeparator = function (separator, callback, context) { var previous = this.setSeparator(separator); var result = callback.call(context); this.setSeparator(previous); return result; }; -Counterpart.prototype._normalizeKeys = function(locale, scope, key, separator) { +Counterpart.prototype._normalizeKeys = function ( + locale, + scope, + key, + separator, +) { var keys = []; keys = keys.concat(this._normalizeKey(locale, separator)); @@ -356,39 +403,46 @@ Counterpart.prototype._normalizeKeys = function(locale, scope, key, separator) { return keys; }; -Counterpart.prototype._normalizeKey = function(key, separator) { - this._registry.normalizedKeys[separator] = this._registry.normalizedKeys[separator] || {}; - - this._registry.normalizedKeys[separator][key] = this._registry.normalizedKeys[separator][key] || (function(key) { - if (Array.isArray(key)) { - var normalizedKeyArray = key.map(function(k) { return this._normalizeKey(k, separator); }.bind(this)); - - return [].concat.apply([], normalizedKeyArray); - } else { - if (typeof key === 'undefined' || key === null) { - return []; - } +Counterpart.prototype._normalizeKey = function (key, separator) { + this._registry.normalizedKeys[separator] = + this._registry.normalizedKeys[separator] || {}; + + this._registry.normalizedKeys[separator][key] = + this._registry.normalizedKeys[separator][key] || + function (key) { + if (Array.isArray(key)) { + var normalizedKeyArray = key.map( + function (k) { + return this._normalizeKey(k, separator); + }.bind(this), + ); + + return [].concat.apply([], normalizedKeyArray); + } else { + if (typeof key === 'undefined' || key === null) { + return []; + } - var keys = key.split(separator); + var keys = key.split(separator); - for (var i = keys.length - 1; i >= 0; i--) { - if (keys[i] === '') { - keys.splice(i, 1); + for (var i = keys.length - 1; i >= 0; i--) { + if (keys[i] === '') { + keys.splice(i, 1); - if (this._registry.keepTrailingDot === true && i == keys.length) { - keys[keys.length - 1] += '' + separator; + if (this._registry.keepTrailingDot === true && i == keys.length) { + keys[keys.length - 1] += '' + separator; + } } } - } - return keys; - } - }.bind(this))(key); + return keys; + } + }.bind(this)(key); return this._registry.normalizedKeys[separator][key]; }; -Counterpart.prototype._interpolate = function(entry, values) { +Counterpart.prototype._interpolate = function (entry, values) { if (typeof entry !== 'string') { return entry; } @@ -397,7 +451,9 @@ Counterpart.prototype._interpolate = function(entry, values) { return sprintf(entry, extend({}, this._registry.interpolations, values)); } catch (err) { if (this._listenerCount('error') > 0) { - this.dispatchEvent(new CustomEvent('error', { detail: { error: err, entry, values }})); + this.dispatchEvent( + new CustomEvent('error', { detail: { error: err, entry, values } }), + ); } else { throw err; } @@ -405,7 +461,13 @@ Counterpart.prototype._interpolate = function(entry, values) { } }; -Counterpart.prototype._resolve = function(locale, scope, object, subject, options) { +Counterpart.prototype._resolve = function ( + locale, + scope, + object, + subject, + options, +) { options = options || {}; if (options.resolve === false) { @@ -415,7 +477,10 @@ Counterpart.prototype._resolve = function(locale, scope, object, subject, option var result; if (isSymbol(subject)) { - result = this.translate(subject, extend({}, options, { locale: locale, scope: scope })); + result = this.translate( + subject, + extend({}, options, { locale: locale, scope: scope }), + ); } else if (isFunction(subject)) { var dateOrTime; @@ -434,13 +499,25 @@ Counterpart.prototype._resolve = function(locale, scope, object, subject, option return /^missing translation:/.test(result) ? null : result; }; -Counterpart.prototype._fallback = function(locale, scope, object, subject, options) { +Counterpart.prototype._fallback = function ( + locale, + scope, + object, + subject, + options, +) { /* jshint unused: false */ var { fallback, ...restOptions } = options; if (Array.isArray(subject)) { for (var i = 0, ii = subject.length; i < ii; i++) { - var result = this._resolve(locale, scope, object, subject[i], restOptions); + var result = this._resolve( + locale, + scope, + object, + subject[i], + restOptions, + ); if (result) { return result; @@ -461,7 +538,7 @@ function translate() { extend(translate, instance, { Instance: Counterpart, - Translator: Counterpart + Translator: Counterpart, }); module.exports = translate; diff --git a/locales/cs.js b/locales/cs.js index 3d11d7e..6ac2764 100644 --- a/locales/cs.js +++ b/locales/cs.js @@ -10,22 +10,22 @@ module.exports = { formats: { date: { - 'default': '%a %e. %b %Y', - long: '%A %e. %B %Y', - short: '%e. %-m. %y' + default: '%a %e. %b %Y', + long: '%A %e. %B %Y', + short: '%e. %-m. %y', }, time: { - 'default': '%-H:%M hod.', - long: '%-H:%M:%S %z', - short: '%-H:%M' + default: '%-H:%M hod.', + long: '%-H:%M:%S %z', + short: '%-H:%M', }, datetime: { - 'default': '%a %e. %b %Y, %-H:%M hod.', - long: '%A %e. %B %Y, %-H:%M:%S %z', - short: '%e. %-m. %y, %-H:%M' - } - } - } + default: '%a %e. %b %Y, %-H:%M hod.', + long: '%A %e. %B %Y, %-H:%M:%S %z', + short: '%e. %-m. %y, %-H:%M', + }, + }, + }, }; diff --git a/locales/de.js b/locales/de.js index b557fc0..2d2b799 100644 --- a/locales/de.js +++ b/locales/de.js @@ -10,22 +10,22 @@ module.exports = { formats: { date: { - 'default': '%a, %e. %b %Y', - long: '%A, %e. %B %Y', - short: '%d.%m.%y' + default: '%a, %e. %b %Y', + long: '%A, %e. %B %Y', + short: '%d.%m.%y', }, time: { - 'default': '%H:%M Uhr', - long: '%H:%M:%S %z', - short: '%H:%M' + default: '%H:%M Uhr', + long: '%H:%M:%S %z', + short: '%H:%M', }, datetime: { - 'default': '%a, %e. %b %Y, %H:%M Uhr', - long: '%A, %e. %B %Y, %H:%M:%S %z', - short: '%d.%m.%y %H:%M' - } - } - } + default: '%a, %e. %b %Y, %H:%M Uhr', + long: '%A, %e. %B %Y, %H:%M:%S %z', + short: '%d.%m.%y %H:%M', + }, + }, + }, }; diff --git a/locales/en.js b/locales/en.js index 36add48..d91f0f9 100644 --- a/locales/en.js +++ b/locales/en.js @@ -9,22 +9,22 @@ module.exports = { formats: { date: { - 'default': '%a, %e %b %Y', - long: '%A, %B %o, %Y', - short: '%b %e' + default: '%a, %e %b %Y', + long: '%A, %B %o, %Y', + short: '%b %e', }, time: { - 'default': '%H:%M', - long: '%H:%M:%S %z', - short: '%H:%M' + default: '%H:%M', + long: '%H:%M:%S %z', + short: '%H:%M', }, datetime: { - 'default': '%a, %e %b %Y %H:%M', - long: '%A, %B %o, %Y %H:%M:%S %z', - short: '%e %b %H:%M' - } - } - } + default: '%a, %e %b %Y %H:%M', + long: '%A, %B %o, %Y %H:%M:%S %z', + short: '%e %b %H:%M', + }, + }, + }, }; diff --git a/locales/nl.js b/locales/nl.js index 5a6c894..06a440b 100644 --- a/locales/nl.js +++ b/locales/nl.js @@ -10,22 +10,22 @@ module.exports = { formats: { date: { - 'default': '%e %b %Y', - long: '%A %e %B %Y', - short: '%Y-%m-%d' + default: '%e %b %Y', + long: '%A %e %B %Y', + short: '%Y-%m-%d', }, time: { - 'default': '%H.%M', - long: '%H.%M.%S', - short: '%H.%M' + default: '%H.%M', + long: '%H.%M.%S', + short: '%H.%M', }, datetime: { - 'default': '%e %b %Y, %H.%M', - long: '%A %e %B %Y, %H.%M.%S', - short: '%Y-%m-%d %H.%M' - } - } - } + default: '%e %b %Y, %H.%M', + long: '%A %e %B %Y, %H.%M.%S', + short: '%Y-%m-%d %H.%M', + }, + }, + }, }; diff --git a/locales/pt-br.js b/locales/pt-br.js index 7c4b063..3a12989 100644 --- a/locales/pt-br.js +++ b/locales/pt-br.js @@ -10,22 +10,22 @@ module.exports = { formats: { date: { - 'default': '%a, %e de %b de %Y', - long: '%A, %e de %B de %Y', - short: '%d/%m/%y' + default: '%a, %e de %b de %Y', + long: '%A, %e de %B de %Y', + short: '%d/%m/%y', }, time: { - 'default': '%H:%M', - long: '%H:%M:%S %z', - short: '%H:%M' + default: '%H:%M', + long: '%H:%M:%S %z', + short: '%H:%M', }, datetime: { - 'default': '%a, %e de %b de %Y às %H:%M', - long: '%A, %e de %B de %Y às %H:%M:%S %z', - short: '%d/%m/%y às %H:%M' - } - } - } + default: '%a, %e de %b de %Y às %H:%M', + long: '%A, %e de %B de %Y às %H:%M:%S %z', + short: '%d/%m/%y às %H:%M', + }, + }, + }, }; diff --git a/locales/ru.js b/locales/ru.js index 011cabe..86f0a87 100644 --- a/locales/ru.js +++ b/locales/ru.js @@ -10,22 +10,22 @@ module.exports = { formats: { date: { - 'default': '%d.%m.%Y', - long: '%A, %d.%m.%Y', - short: '%d.%m.%Y' + default: '%d.%m.%Y', + long: '%A, %d.%m.%Y', + short: '%d.%m.%Y', }, time: { - 'default': '%H:%M', - long: '%H:%M:%S', - short: '%H:%M' + default: '%H:%M', + long: '%H:%M:%S', + short: '%H:%M', }, datetime: { - 'default': '%d.%m.%Y, %H:%M', - long: '%A, %d.%m.%Y, %H:%M:%S', - short: '%d.%m.%Y %H:%M' - } - } - } + default: '%d.%m.%Y, %H:%M', + long: '%A, %d.%m.%Y, %H:%M:%S', + short: '%d.%m.%Y %H:%M', + }, + }, + }, }; diff --git a/locales/sk.js b/locales/sk.js index ce6fec3..80d77a8 100644 --- a/locales/sk.js +++ b/locales/sk.js @@ -10,22 +10,22 @@ module.exports = { formats: { date: { - 'default': '%a %e. %b %Y', - long: '%A %e. %B %Y', - short: '%e. %-m. %y' + default: '%a %e. %b %Y', + long: '%A %e. %B %Y', + short: '%e. %-m. %y', }, time: { - 'default': '%-H:%M hod.', - long: '%-H:%M:%S %z', - short: '%-H:%M' + default: '%-H:%M hod.', + long: '%-H:%M:%S %z', + short: '%-H:%M', }, datetime: { - 'default': '%a %e. %b %Y, %-H:%M hod.', - long: '%A %e. %B %Y, %-H:%M:%S %z', - short: '%e. %-m. %y, %-H:%M' - } - } - } + default: '%a %e. %b %Y, %-H:%M hod.', + long: '%A %e. %B %Y, %-H:%M:%S %z', + short: '%e. %-m. %y, %-H:%M', + }, + }, + }, }; diff --git a/package-lock.json b/package-lock.json index 1825201..5d15b67 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "devDependencies": { "jshint": "^2.13.6", "mocha": "^2.0.1", + "prettier": "^3.3.3", "time": "github:riyadshauk/node-time" } }, @@ -462,6 +463,22 @@ "resolved": "https://registry.npmjs.org/pluralizers/-/pluralizers-0.1.7.tgz", "integrity": "sha512-mw6AejUiCaMQ6uPN9ObjJDTnR5AnBSmnHHy3uVTbxrSFSxO5scfwpTs8Dxyb6T2v7GSulhvOq+pm9y+hXUvtOA==" }, + "node_modules/prettier": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", + "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/readable-stream": { "version": "1.1.14", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", diff --git a/package.json b/package.json index 8c0b349..f1fe8e5 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,8 @@ ], "main": "index.js", "scripts": { + "format:check": "prettier --check --cache --cache-location=.cache/prettier \"**/*.js\"", + "format:write": "prettier --write --cache --cache-location=.cache/prettier \"**/*.js\"", "test": "make test", "lint": "make lint" }, @@ -57,6 +59,7 @@ "devDependencies": { "jshint": "^2.13.6", "mocha": "^2.0.1", + "prettier": "^3.3.3", "time": "github:riyadshauk/node-time" } } diff --git a/spec.js b/spec.js index 06e3c0e..48d107b 100644 --- a/spec.js +++ b/spec.js @@ -3,402 +3,816 @@ var time = require('time'); var translate = require('./'); var Translator = translate.Translator; -describe('translate', function() { +describe('translate', function () { var instance; - beforeEach(function() { + beforeEach(function () { instance = new Translator(); }); - it('is a function', function() { + it('is a function', function () { assert.isFunction(instance.translate); }); - it('is backward-compatible', function() { + it('is backward-compatible', function () { assert.isFunction(translate); assert.isFunction(translate.translate); }); - describe('when called', function() { - describe('with a non-empty string or an array as first argument', function() { - it('does not throw an invalid argument error', function() { - assert.doesNotThrow(function() { instance.translate('foo'); }, /invalid argument/); - assert.doesNotThrow(function() { instance.translate(['foo']); }, /invalid argument/); + describe('when called', function () { + describe('with a non-empty string or an array as first argument', function () { + it('does not throw an invalid argument error', function () { + assert.doesNotThrow(function () { + instance.translate('foo'); + }, /invalid argument/); + assert.doesNotThrow(function () { + instance.translate(['foo']); + }, /invalid argument/); }); - describe('with the default locale present', function() { - describe('without a current scope or provided scope option', function() { - it('generates the correct normalized keys', function() { - assert.equal(instance.translate('foo'), 'missing translation: en.foo'); + describe('with the default locale present', function () { + describe('without a current scope or provided scope option', function () { + it('generates the correct normalized keys', function () { + assert.equal( + instance.translate('foo'), + 'missing translation: en.foo', + ); }); }); - describe('with a current scope present', function() { - it('generates the correct normalized keys', function() { - instance.withScope('other', function() { - assert.equal(instance.translate('foo'), 'missing translation: en.other.foo'); + describe('with a current scope present', function () { + it('generates the correct normalized keys', function () { + instance.withScope('other', function () { + assert.equal( + instance.translate('foo'), + 'missing translation: en.other.foo', + ); }); }); }); - describe('with a scope provided as option', function() { - it('generates the correct normalized keys', function() { - assert.equal(instance.translate('foo', { scope: 'other' }), 'missing translation: en.other.foo'); + describe('with a scope provided as option', function () { + it('generates the correct normalized keys', function () { + assert.equal( + instance.translate('foo', { scope: 'other' }), + 'missing translation: en.other.foo', + ); }); }); }); - describe('with a different locale present', function() { - describe('without a current scope or provided scope option', function() { - it('generates the correct normalized keys', function() { - instance.withLocale('de', function() { - assert.equal(instance.translate('foo'), 'missing translation: de.foo'); + describe('with a different locale present', function () { + describe('without a current scope or provided scope option', function () { + it('generates the correct normalized keys', function () { + instance.withLocale('de', function () { + assert.equal( + instance.translate('foo'), + 'missing translation: de.foo', + ); }); }); }); - describe('with a current scope present', function() { - it('generates the correct normalized keys', function() { - instance.withLocale('de', function() { - instance.withScope('other', function() { - assert.equal(instance.translate('foo'), 'missing translation: de.other.foo'); + describe('with a current scope present', function () { + it('generates the correct normalized keys', function () { + instance.withLocale('de', function () { + instance.withScope('other', function () { + assert.equal( + instance.translate('foo'), + 'missing translation: de.other.foo', + ); }); }); }); }); - describe('with a scope provided as option', function() { - it('generates the correct normalized keys', function() { - instance.withLocale('de', function() { - assert.equal(instance.translate('foo', { scope: 'other' }), 'missing translation: de.other.foo'); + describe('with a scope provided as option', function () { + it('generates the correct normalized keys', function () { + instance.withLocale('de', function () { + assert.equal( + instance.translate('foo', { scope: 'other' }), + 'missing translation: de.other.foo', + ); }); }); }); }); - describe('with a locale provided as option', function() { - describe('without a current scope or provided scope option', function() { - it('generates the correct normalized keys', function() { - assert.equal(instance.translate('foo', { locale: 'de' }), 'missing translation: de.foo'); + describe('with a locale provided as option', function () { + describe('without a current scope or provided scope option', function () { + it('generates the correct normalized keys', function () { + assert.equal( + instance.translate('foo', { locale: 'de' }), + 'missing translation: de.foo', + ); }); }); - describe('with a current scope present', function() { - it('generates the correct normalized keys', function() { - instance.withScope('other', function() { - assert.equal(instance.translate('foo', { locale: 'de' }), 'missing translation: de.other.foo'); + describe('with a current scope present', function () { + it('generates the correct normalized keys', function () { + instance.withScope('other', function () { + assert.equal( + instance.translate('foo', { locale: 'de' }), + 'missing translation: de.other.foo', + ); }); }); }); - describe('with a scope provided as option', function() { - it('generates the correct normalized keys', function() { - assert.equal(instance.translate('foo', { locale: 'de', scope: 'other' }), 'missing translation: de.other.foo'); + describe('with a scope provided as option', function () { + it('generates the correct normalized keys', function () { + assert.equal( + instance.translate('foo', { locale: 'de', scope: 'other' }), + 'missing translation: de.other.foo', + ); }); }); }); - describe('with options provided', function() { - it('does not mutate these options', function() { - var options = { locale: 'en', scope: ['foo1', 'foo2'], count: 3, bar: { baz: 'bum' } }; + describe('with options provided', function () { + it('does not mutate these options', function () { + var options = { + locale: 'en', + scope: ['foo1', 'foo2'], + count: 3, + bar: { baz: 'bum' }, + }; instance.translate('boing', options); - assert.deepEqual(options, { locale: 'en', scope: ['foo1', 'foo2'], count: 3, bar: { baz: 'bum' } }); + assert.deepEqual(options, { + locale: 'en', + scope: ['foo1', 'foo2'], + count: 3, + bar: { baz: 'bum' }, + }); }); }); - describe('with a translation for the key present', function() { - it('returns that translation', function() { - instance.registerTranslations('en', { foo: { bar: { baz: { bam: 'boo' } } } }); + describe('with a translation for the key present', function () { + it('returns that translation', function () { + instance.registerTranslations('en', { + foo: { bar: { baz: { bam: 'boo' } } }, + }); // strings - assert.equal(instance.translate('foo.bar.baz.bam'), 'boo'); - assert.equal(instance.translate('bar.baz.bam', { scope: 'foo' }), 'boo'); - assert.equal(instance.translate('baz.bam', { scope: 'foo.bar' }), 'boo'); - assert.equal(instance.translate('bam', { scope: 'foo.bar.baz' }), 'boo'); + assert.equal(instance.translate('foo.bar.baz.bam'), 'boo'); + assert.equal( + instance.translate('bar.baz.bam', { scope: 'foo' }), + 'boo', + ); + assert.equal( + instance.translate('baz.bam', { scope: 'foo.bar' }), + 'boo', + ); + assert.equal( + instance.translate('bam', { scope: 'foo.bar.baz' }), + 'boo', + ); // arrays - assert.equal(instance.translate(['foo', 'bar', 'baz', 'bam']), 'boo'); - assert.equal(instance.translate(['bar', 'baz', 'bam'], { scope: ['foo'] }), 'boo'); - assert.equal(instance.translate(['baz', 'bam'], { scope: ['foo', 'bar'] }), 'boo'); - assert.equal(instance.translate(['bam'], { scope: ['foo', 'bar', 'baz'] }), 'boo'); + assert.equal(instance.translate(['foo', 'bar', 'baz', 'bam']), 'boo'); + assert.equal( + instance.translate(['bar', 'baz', 'bam'], { scope: ['foo'] }), + 'boo', + ); + assert.equal( + instance.translate(['baz', 'bam'], { scope: ['foo', 'bar'] }), + 'boo', + ); + assert.equal( + instance.translate(['bam'], { scope: ['foo', 'bar', 'baz'] }), + 'boo', + ); // mixed - assert.equal(instance.translate(['foo.bar', 'baz', 'bam']), 'boo'); - assert.equal(instance.translate(['bar', 'baz.bam'], { scope: 'foo' }), 'boo'); - assert.equal(instance.translate(['baz', 'bam'], { scope: 'foo.bar' }), 'boo'); - assert.equal(instance.translate('bam', { scope: ['foo.bar', 'baz'] }), 'boo'); + assert.equal(instance.translate(['foo.bar', 'baz', 'bam']), 'boo'); + assert.equal( + instance.translate(['bar', 'baz.bam'], { scope: 'foo' }), + 'boo', + ); + assert.equal( + instance.translate(['baz', 'bam'], { scope: 'foo.bar' }), + 'boo', + ); + assert.equal( + instance.translate('bam', { scope: ['foo.bar', 'baz'] }), + 'boo', + ); // strange looking - assert.equal(instance.translate(['..foo.bar', 'baz', '', 'bam']), 'boo'); - assert.equal(instance.translate(['bar', 'baz..bam.'], { scope: '.foo' }), 'boo'); - assert.equal(instance.translate(['baz', null, 'bam'], { scope: 'foo.bar.' }), 'boo'); - assert.equal(instance.translate('bam...', { scope: [null, 'foo..bar', '', 'baz'] }), 'boo'); - }); - - describe('with a `count` provided as option', function() { - it('correctly pluralizes the translated value', function() { - instance.registerTranslations('en', { foo: { zero: 'no items', one: 'one item', other: '%(count)s items' } }); + assert.equal( + instance.translate(['..foo.bar', 'baz', '', 'bam']), + 'boo', + ); + assert.equal( + instance.translate(['bar', 'baz..bam.'], { scope: '.foo' }), + 'boo', + ); + assert.equal( + instance.translate(['baz', null, 'bam'], { scope: 'foo.bar.' }), + 'boo', + ); + assert.equal( + instance.translate('bam...', { + scope: [null, 'foo..bar', '', 'baz'], + }), + 'boo', + ); + }); + + describe('with a `count` provided as option', function () { + it('correctly pluralizes the translated value', function () { + instance.registerTranslations('en', { + foo: { + zero: 'no items', + one: 'one item', + other: '%(count)s items', + }, + }); - assert.equal(instance.translate('foo', { count: 0 }), 'no items'); - assert.equal(instance.translate('foo', { count: 1 }), 'one item'); - assert.equal(instance.translate('foo', { count: 2 }), '2 items'); - assert.equal(instance.translate('foo', { count: 42 }), '42 items'); + assert.equal(instance.translate('foo', { count: 0 }), 'no items'); + assert.equal(instance.translate('foo', { count: 1 }), 'one item'); + assert.equal(instance.translate('foo', { count: 2 }), '2 items'); + assert.equal(instance.translate('foo', { count: 42 }), '42 items'); }); }); - describe('with a `separator` provided as option', function() { - it('correctly returns single array with key', function() { + describe('with a `separator` provided as option', function () { + it('correctly returns single array with key', function () { instance.registerTranslations('en', { - 'long.key.with.dots.in.name': 'Key with dots doesn\'t get split and returns correctly', + 'long.key.with.dots.in.name': + "Key with dots doesn't get split and returns correctly", another: { - key: 'bar' + key: 'bar', }, mixed: { 'dots.and': { - separator: 'bingo' - } - } + separator: 'bingo', + }, + }, }); - assert.equal(instance.translate('long.key.with.dots.in.name', { separator: '-' }), 'Key with dots doesn\'t get split and returns correctly'); - assert.equal(instance.translate('long.key.with.dots.in.name.not-found', { separator: '-' }), 'missing translation: en-long.key.with.dots.in.name.not-found'); - assert.equal(instance.translate('another-key', { separator: '-' }), 'bar'); - assert.equal(instance.translate('mixed-dots.and-separator', { separator: '-' }), 'bingo'); - }); - - it('correctly returns nested key when using `*` as seperator', function() { - instance.registerTranslations('en', { "long": { key: { "with": { dots: { "in": { name: 'boo' } } } }} }); + assert.equal( + instance.translate('long.key.with.dots.in.name', { + separator: '-', + }), + "Key with dots doesn't get split and returns correctly", + ); + assert.equal( + instance.translate('long.key.with.dots.in.name.not-found', { + separator: '-', + }), + 'missing translation: en-long.key.with.dots.in.name.not-found', + ); + assert.equal( + instance.translate('another-key', { separator: '-' }), + 'bar', + ); + assert.equal( + instance.translate('mixed-dots.and-separator', { + separator: '-', + }), + 'bingo', + ); + }); + + it('correctly returns nested key when using `*` as seperator', function () { + instance.registerTranslations('en', { + long: { key: { with: { dots: { in: { name: 'boo' } } } } }, + }); - assert.equal(instance.translate('long*key*with*dots*in*name', { separator: '*' }), 'boo'); + assert.equal( + instance.translate('long*key*with*dots*in*name', { + separator: '*', + }), + 'boo', + ); }); }); - describe('with other options provided', function() { - describe('by default', function() { - it('interpolates these options into the translated value', function() { - instance.registerTranslations('en', { foo: 'Hi %(name)s! See you %(when)s!' }); - assert.equal(instance.translate('foo', { name: 'Paul', when: 'later', where: 'home' }), 'Hi Paul! See you later!'); - - instance.registerTranslations('en', { foo: 'Hello %(users[0].name)s and %(users[1].name)s!' }); - assert.equal(instance.translate('foo', { users: [{ name: 'Molly' }, { name: 'Polly' }] }), 'Hello Molly and Polly!'); + describe('with other options provided', function () { + describe('by default', function () { + it('interpolates these options into the translated value', function () { + instance.registerTranslations('en', { + foo: 'Hi %(name)s! See you %(when)s!', + }); + assert.equal( + instance.translate('foo', { + name: 'Paul', + when: 'later', + where: 'home', + }), + 'Hi Paul! See you later!', + ); + + instance.registerTranslations('en', { + foo: 'Hello %(users[0].name)s and %(users[1].name)s!', + }); + assert.equal( + instance.translate('foo', { + users: [{ name: 'Molly' }, { name: 'Polly' }], + }), + 'Hello Molly and Polly!', + ); }); - it('interpolates the registered interpolations into the translated value', function() { + it('interpolates the registered interpolations into the translated value', function () { var current = instance._registry.interpolations; - instance.registerTranslations('en', {'hello':'Hello from %(brand)s!'}); - instance.registerInterpolations({brand:'Z'}); + instance.registerTranslations('en', { + hello: 'Hello from %(brand)s!', + }); + instance.registerInterpolations({ brand: 'Z' }); assert.equal(instance.translate('hello'), 'Hello from Z!'); instance._registry.interpolations = current; - instance.registerInterpolations({ app_name: 'My Cool App', question: 'How are you today?' }); - instance.registerTranslations('en', { greeting: 'Welcome to %(app_name)s, %(name)s! %(question)s' }); + instance.registerInterpolations({ + app_name: 'My Cool App', + question: 'How are you today?', + }); + instance.registerTranslations('en', { + greeting: 'Welcome to %(app_name)s, %(name)s! %(question)s', + }); - assert.equal(instance.translate('greeting', { name: 'Martin' }), 'Welcome to My Cool App, Martin! How are you today?'); - assert.equal(instance.translate('greeting', { name: 'Martin', app_name: 'The Foo App' }), 'Welcome to The Foo App, Martin! How are you today?'); + assert.equal( + instance.translate('greeting', { name: 'Martin' }), + 'Welcome to My Cool App, Martin! How are you today?', + ); + assert.equal( + instance.translate('greeting', { + name: 'Martin', + app_name: 'The Foo App', + }), + 'Welcome to The Foo App, Martin! How are you today?', + ); instance._registry.interpolations = current; }); }); - describe('with the `interpolate` options set to `false`', function() { - it('interpolates these options into the translated value', function() { - instance.registerTranslations('en', { foo: 'Hi %(name)s! See you %(when)s!' }); - assert.equal(instance.translate('foo', { interpolate: false, name: 'Paul', when: 'later', where: 'home' }), 'Hi %(name)s! See you %(when)s!'); + describe('with the `interpolate` options set to `false`', function () { + it('interpolates these options into the translated value', function () { + instance.registerTranslations('en', { + foo: 'Hi %(name)s! See you %(when)s!', + }); + assert.equal( + instance.translate('foo', { + interpolate: false, + name: 'Paul', + when: 'later', + where: 'home', + }), + 'Hi %(name)s! See you %(when)s!', + ); }); }); }); - describe('with the keepTrailingDot setting set to true', function() { - it('returns the translation for keys that contain a trailing dot', function() { - instance.registerTranslations('fr', { foo: { bar: 'baz', 'With a dot.': 'Avec un point.' }, 'dot.': 'point.' }); + describe('with the keepTrailingDot setting set to true', function () { + it('returns the translation for keys that contain a trailing dot', function () { + instance.registerTranslations('fr', { + foo: { bar: 'baz', 'With a dot.': 'Avec un point.' }, + 'dot.': 'point.', + }); instance._registry.keepTrailingDot = true; - instance.withLocale('fr', function() { - assert.equal(instance.translate('foo.bar'), 'baz'); - assert.equal(instance.translate('foo.With a dot.'), 'Avec un point.'); - assert.equal(instance.translate('dot.'), 'point.'); - - assert.equal(instance.translate('foo..bar'), 'baz'); - assert.equal(instance.translate('foo..With a dot.'), 'Avec un point.'); - assert.equal(instance.translate('.dot.'), 'point.'); - - assert.equal(instance.translate('foo.bar.'), 'missing translation: fr.foo.bar.'); - assert.equal(instance.translate('foo.With a dot..'), 'missing translation: fr.foo.With a dot..'); - assert.equal(instance.translate('foo.With. a dot.'), 'missing translation: fr.foo.With. a dot.'); - assert.equal(instance.translate('dot..'), 'missing translation: fr.dot..'); + instance.withLocale('fr', function () { + assert.equal(instance.translate('foo.bar'), 'baz'); + assert.equal( + instance.translate('foo.With a dot.'), + 'Avec un point.', + ); + assert.equal(instance.translate('dot.'), 'point.'); + + assert.equal(instance.translate('foo..bar'), 'baz'); + assert.equal( + instance.translate('foo..With a dot.'), + 'Avec un point.', + ); + assert.equal(instance.translate('.dot.'), 'point.'); + + assert.equal( + instance.translate('foo.bar.'), + 'missing translation: fr.foo.bar.', + ); + assert.equal( + instance.translate('foo.With a dot..'), + 'missing translation: fr.foo.With a dot..', + ); + assert.equal( + instance.translate('foo.With. a dot.'), + 'missing translation: fr.foo.With. a dot.', + ); + assert.equal( + instance.translate('dot..'), + 'missing translation: fr.dot..', + ); }); }); }); }); - describe('with a translation for a prefix of the key present', function() { - it('returns the remaining translation part', function() { - instance.registerTranslations('en', { foo: { bar: { baz: { zero: 'no items', one: 'one item', other: '%(count)s items' } } } }); - assert.deepEqual(instance.translate('baz', { scope: ['foo', 'bar'] }), { zero: 'no items', one: 'one item', other: '%(count)s items' }); + describe('with a translation for a prefix of the key present', function () { + it('returns the remaining translation part', function () { + instance.registerTranslations('en', { + foo: { + bar: { + baz: { + zero: 'no items', + one: 'one item', + other: '%(count)s items', + }, + }, + }, + }); + assert.deepEqual( + instance.translate('baz', { scope: ['foo', 'bar'] }), + { zero: 'no items', one: 'one item', other: '%(count)s items' }, + ); }); }); - describe('with an array-type translation for the key present', function() { - it('returns the array that key points to', function() { - instance.registerTranslations('en', { foo: { bar: { baz: [1, 'A', 0.42] } } }); - assert.deepEqual(instance.translate(['bar', 'baz'], { scope: 'foo' }), [1, 'A', 0.42]); + describe('with an array-type translation for the key present', function () { + it('returns the array that key points to', function () { + instance.registerTranslations('en', { + foo: { bar: { baz: [1, 'A', 0.42] } }, + }); + assert.deepEqual( + instance.translate(['bar', 'baz'], { scope: 'foo' }), + [1, 'A', 0.42], + ); }); }); - describe('with a function-type translation for the key present', function() { - it('returns the array that key points to', function() { - var myFunc = function() {}; + describe('with a function-type translation for the key present', function () { + it('returns the array that key points to', function () { + var myFunc = function () {}; - instance.registerTranslations('en', { foo: { bar: { baz: myFunc } } }); - assert.equal(instance.translate(['bar', 'baz'], { scope: 'foo' }), myFunc); + instance.registerTranslations('en', { + foo: { bar: { baz: myFunc } }, + }); + assert.equal( + instance.translate(['bar', 'baz'], { scope: 'foo' }), + myFunc, + ); }); }); - describe('with a function-type fallback present', function() { - it('returns the array that key points to', function() { - var myFunc = function() { return 'Here I am!'; }; - var myFunc2 = function(x) { return 'Here ' + x + ' are!'; }; - var fallbacks = [':i_dont_exist_either', myFunc, 'Should not be returned']; - - assert.equal(instance.translate('i_dont_exist', { fallback: myFunc }), 'Here I am!'); - assert.equal(instance.translate('i_dont_exist', { fallback: myFunc2, object: 'you' }), 'Here you are!'); - assert.equal(instance.translate('i_dont_exist', { fallback: myFunc2 }), 'Here i_dont_exist are!'); - assert.equal(instance.translate('i_dont_exist', { fallback: fallbacks }), 'Here I am!'); + describe('with a function-type fallback present', function () { + it('returns the array that key points to', function () { + var myFunc = function () { + return 'Here I am!'; + }; + var myFunc2 = function (x) { + return 'Here ' + x + ' are!'; + }; + var fallbacks = [ + ':i_dont_exist_either', + myFunc, + 'Should not be returned', + ]; + + assert.equal( + instance.translate('i_dont_exist', { fallback: myFunc }), + 'Here I am!', + ); + assert.equal( + instance.translate('i_dont_exist', { + fallback: myFunc2, + object: 'you', + }), + 'Here you are!', + ); + assert.equal( + instance.translate('i_dont_exist', { fallback: myFunc2 }), + 'Here i_dont_exist are!', + ); + assert.equal( + instance.translate('i_dont_exist', { fallback: fallbacks }), + 'Here I am!', + ); }); }); - describe('without a translation for the key present', function() { - it('returns a string "missing translation: %(locale).%(scope).%(key)"', function() { - assert.deepEqual(instance.translate('bar', { locale: 'unknown', scope: 'foo' }), 'missing translation: unknown.foo.bar'); - }); - - describe('with a `fallback` provided as option', function() { - it('returns the fallback', function() { - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar', fallback: 'boom' }), 'boom'); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar', fallback: 'Hello, %(name)s!', name: 'Martin' }), 'Hello, Martin!'); - - assert.equal(instance.translate('bazz', { locale: 'en', scope: 'bar', fallback: { zero: 'no items', one: 'one item', other: '%(count)s items' }, count: 0 }), 'no items'); - assert.equal(instance.translate('bazz', { locale: 'en', scope: 'bar', fallback: { zero: 'no items', one: 'one item', other: '%(count)s items' }, count: 1 }), 'one item'); - assert.equal(instance.translate('bazz', { locale: 'en', scope: 'bar', fallback: { zero: 'no items', one: 'one item', other: '%(count)s items' }, count: 2 }), '2 items'); - - assert.deepEqual(instance.translate('baz', { locale: 'foo', scope: 'bar', fallback: { oh: 'yeah' } }), { oh: 'yeah' }); - assert.deepEqual(instance.translate('baz', { locale: 'foo', scope: 'bar', fallback: [1, 'A', 0.42] }), 1); - }); - - it('translates the fallback if given as "symbol" or array', function() { - instance.registerTranslations('en', { foo: { bar: 'bar', baz: 'baz' } }); - - assert.equal(instance.translate('missing', { fallback: 'default' }), 'default'); - assert.equal(instance.translate('missing', { fallback: ':foo.bar' }), 'bar'); - assert.equal(instance.translate('missing', { fallback: ':bar', scope: 'foo' }), 'bar'); - assert.equal(instance.translate('missing', { fallback: [':also_missing', ':foo.bar'] }), 'bar'); - assert.matches(instance.translate('missing', { fallback: [':also_missing', ':foo.missed'] }), /missing translation/); - }); - }); - - describe('with a global `fallbackLocale` present', function() { - it('returns the entry of the fallback locale', function() { + describe('without a translation for the key present', function () { + it('returns a string "missing translation: %(locale).%(scope).%(key)"', function () { + assert.deepEqual( + instance.translate('bar', { locale: 'unknown', scope: 'foo' }), + 'missing translation: unknown.foo.bar', + ); + }); + + describe('with a `fallback` provided as option', function () { + it('returns the fallback', function () { + assert.equal( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallback: 'boom', + }), + 'boom', + ); + assert.equal( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallback: 'Hello, %(name)s!', + name: 'Martin', + }), + 'Hello, Martin!', + ); + + assert.equal( + instance.translate('bazz', { + locale: 'en', + scope: 'bar', + fallback: { + zero: 'no items', + one: 'one item', + other: '%(count)s items', + }, + count: 0, + }), + 'no items', + ); + assert.equal( + instance.translate('bazz', { + locale: 'en', + scope: 'bar', + fallback: { + zero: 'no items', + one: 'one item', + other: '%(count)s items', + }, + count: 1, + }), + 'one item', + ); + assert.equal( + instance.translate('bazz', { + locale: 'en', + scope: 'bar', + fallback: { + zero: 'no items', + one: 'one item', + other: '%(count)s items', + }, + count: 2, + }), + '2 items', + ); + + assert.deepEqual( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallback: { oh: 'yeah' }, + }), + { oh: 'yeah' }, + ); + assert.deepEqual( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallback: [1, 'A', 0.42], + }), + 1, + ); + }); + + it('translates the fallback if given as "symbol" or array', function () { + instance.registerTranslations('en', { + foo: { bar: 'bar', baz: 'baz' }, + }); + + assert.equal( + instance.translate('missing', { fallback: 'default' }), + 'default', + ); + assert.equal( + instance.translate('missing', { fallback: ':foo.bar' }), + 'bar', + ); + assert.equal( + instance.translate('missing', { fallback: ':bar', scope: 'foo' }), + 'bar', + ); + assert.equal( + instance.translate('missing', { + fallback: [':also_missing', ':foo.bar'], + }), + 'bar', + ); + assert.matches( + instance.translate('missing', { + fallback: [':also_missing', ':foo.missed'], + }), + /missing translation/, + ); + }); + }); + + describe('with a global `fallbackLocale` present', function () { + it('returns the entry of the fallback locale', function () { instance.registerTranslations('de', { bar: { baz: 'bam' } }); instance.registerTranslations('de', { hello: 'Hallo %(name)s!' }); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar' }), 'missing translation: foo.bar.baz'); - assert.equal(instance.translate('hello', { locale: 'foo', name: 'Martin' }), 'missing translation: foo.hello'); + assert.equal( + instance.translate('baz', { locale: 'foo', scope: 'bar' }), + 'missing translation: foo.bar.baz', + ); + assert.equal( + instance.translate('hello', { locale: 'foo', name: 'Martin' }), + 'missing translation: foo.hello', + ); var previousFallbackLocale = instance.setFallbackLocale('de'); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar' }), 'bam'); - assert.equal(instance.translate('hello', { locale: 'foo', name: 'Martin' }), 'Hallo Martin!'); + assert.equal( + instance.translate('baz', { locale: 'foo', scope: 'bar' }), + 'bam', + ); + assert.equal( + instance.translate('hello', { locale: 'foo', name: 'Martin' }), + 'Hallo Martin!', + ); instance.setFallbackLocale(previousFallbackLocale); }); }); - describe('with multiple global `fallbackLocales` present', function() { - it('returns the entry of the last fallback locale', function() { + describe('with multiple global `fallbackLocales` present', function () { + it('returns the entry of the last fallback locale', function () { instance.registerTranslations('de', { bar: { baz: 'bam' } }); instance.registerTranslations('de', { hello: 'Hallo %(name)s!' }); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar' }), 'missing translation: foo.bar.baz'); - assert.equal(instance.translate('hello', { locale: 'foo', name: 'Martin' }), 'missing translation: foo.hello'); - - var previousFallbackLocale = instance.setFallbackLocale([ 'bar', 'de' ]); - - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar' }), 'bam'); - assert.equal(instance.translate('hello', { locale: 'foo', name: 'Martin' }), 'Hallo Martin!'); + assert.equal( + instance.translate('baz', { locale: 'foo', scope: 'bar' }), + 'missing translation: foo.bar.baz', + ); + assert.equal( + instance.translate('hello', { locale: 'foo', name: 'Martin' }), + 'missing translation: foo.hello', + ); + + var previousFallbackLocale = instance.setFallbackLocale([ + 'bar', + 'de', + ]); + + assert.equal( + instance.translate('baz', { locale: 'foo', scope: 'bar' }), + 'bam', + ); + assert.equal( + instance.translate('hello', { locale: 'foo', name: 'Martin' }), + 'Hallo Martin!', + ); instance.setFallbackLocale(previousFallbackLocale); }); - it('returns the entry of the first fallback locale to have an entry', function() { + it('returns the entry of the first fallback locale to have an entry', function () { instance.registerTranslations('de', { bar: { baz: 'bam' } }); instance.registerTranslations('de', { hello: 'Hallo %(name)s!' }); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar' }), 'missing translation: foo.bar.baz'); - assert.equal(instance.translate('hello', { locale: 'foo', name: 'Martin' }), 'missing translation: foo.hello'); - - var previousFallbackLocale = instance.setFallbackLocale([ 'bar', 'de', 'baz' ]); - - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar' }), 'bam'); - assert.equal(instance.translate('hello', { locale: 'foo', name: 'Martin' }), 'Hallo Martin!'); + assert.equal( + instance.translate('baz', { locale: 'foo', scope: 'bar' }), + 'missing translation: foo.bar.baz', + ); + assert.equal( + instance.translate('hello', { locale: 'foo', name: 'Martin' }), + 'missing translation: foo.hello', + ); + + var previousFallbackLocale = instance.setFallbackLocale([ + 'bar', + 'de', + 'baz', + ]); + + assert.equal( + instance.translate('baz', { locale: 'foo', scope: 'bar' }), + 'bam', + ); + assert.equal( + instance.translate('hello', { locale: 'foo', name: 'Martin' }), + 'Hallo Martin!', + ); instance.setFallbackLocale(previousFallbackLocale); }); }); - describe('with a `fallbackLocale` provided as option', function() { - it('returns the entry of the fallback locale', function() { + describe('with a `fallbackLocale` provided as option', function () { + it('returns the entry of the fallback locale', function () { instance.registerTranslations('en', { bar: { baz: 'bam' } }); instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar', fallbackLocale: 'en' }), 'bam'); - assert.equal(instance.translate('hello', { locale: 'foo', fallbackLocale: 'en', name: 'Martin' }), 'Hello, Martin!'); - }); - }); - - describe('with multiple `fallbackLocales` provided as option', function() { - it('returns the entry of the last fallback locale', function() { + assert.equal( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallbackLocale: 'en', + }), + 'bam', + ); + assert.equal( + instance.translate('hello', { + locale: 'foo', + fallbackLocale: 'en', + name: 'Martin', + }), + 'Hello, Martin!', + ); + }); + }); + + describe('with multiple `fallbackLocales` provided as option', function () { + it('returns the entry of the last fallback locale', function () { instance.registerTranslations('en', { bar: { baz: 'bam' } }); instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar', fallbackLocale: ['bar', 'en'] }), 'bam'); - assert.equal(instance.translate('hello', { locale: 'foo', fallbackLocale: ['bar', 'en'], name: 'Martin' }), 'Hello, Martin!'); - }); - - it('returns the entry of the first fallback locale that has an entry', function() { + assert.equal( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallbackLocale: ['bar', 'en'], + }), + 'bam', + ); + assert.equal( + instance.translate('hello', { + locale: 'foo', + fallbackLocale: ['bar', 'en'], + name: 'Martin', + }), + 'Hello, Martin!', + ); + }); + + it('returns the entry of the first fallback locale that has an entry', function () { instance.registerTranslations('en', { bar: { baz: 'bam' } }); instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); - assert.equal(instance.translate('baz', { locale: 'foo', scope: 'bar', fallbackLocale: ['bar', 'en', 'baz'] }), 'bam'); - assert.equal(instance.translate('hello', { locale: 'foo', fallbackLocale: ['bar', 'en', 'baz'], name: 'Martin' }), 'Hello, Martin!'); + assert.equal( + instance.translate('baz', { + locale: 'foo', + scope: 'bar', + fallbackLocale: ['bar', 'en', 'baz'], + }), + 'bam', + ); + assert.equal( + instance.translate('hello', { + locale: 'foo', + fallbackLocale: ['bar', 'en', 'baz'], + name: 'Martin', + }), + 'Hello, Martin!', + ); }); }); }); }); - describe('without a valid key as first argument', function() { - it('throws an invalid argument error', function() { - var keys = [undefined, null, 42, {}, new Date(), /./, function() {}, [], '']; + describe('without a valid key as first argument', function () { + it('throws an invalid argument error', function () { + var keys = [ + undefined, + null, + 42, + {}, + new Date(), + /./, + function () {}, + [], + '', + ]; for (var i = 0, ii = keys.length; i < ii; i++) { - assert.throws(function() { instance.translate(keys[i]); }, /invalid argument/); + assert.throws(function () { + instance.translate(keys[i]); + }, /invalid argument/); } }); }); - describe('with global interpolate setting set to false', function() { - it('will not interpolate', function() { + describe('with global interpolate setting set to false', function () { + it('will not interpolate', function () { var current = instance._registry.interpolations; - instance.registerTranslations('en', { 'hello':'Hello from %(brand)s!' }); + instance.registerTranslations('en', { hello: 'Hello from %(brand)s!' }); instance.registerInterpolations({ brand: 'Z' }); assert.equal(instance.translate('hello'), 'Hello from Z!'); var prev = instance.setInterpolate(false); assert.equal(instance.translate('hello'), 'Hello from %(brand)s!'); - assert.equal(instance.translate('hello', { interpolate: true }), 'Hello from %(brand)s!'); + assert.equal( + instance.translate('hello', { interpolate: true }), + 'Hello from %(brand)s!', + ); instance.setInterpolate(prev); instance._registry.interpolations = current; @@ -406,54 +820,58 @@ describe('translate', function() { }); }); - describe('#translate', function() { - it('is a function', function() { + describe('#translate', function () { + it('is a function', function () { assert.isFunction(instance.translate); }); }); - describe('#getLocale', function() { - it('is a function', function() { + describe('#getLocale', function () { + it('is a function', function () { assert.isFunction(instance.getLocale); }); - it('returns the locale stored in the registry', function() { + it('returns the locale stored in the registry', function () { assert.equal(instance.getLocale(), instance._registry.locale); }); - it('returns "en" by default', function() { + it('returns "en" by default', function () { assert.equal(instance.getLocale(), 'en'); }); }); - describe('#setLocale', function() { - it('is a function', function() { + describe('#setLocale', function () { + it('is a function', function () { assert.isFunction(instance.setLocale); }); - it('sets the locale stored in the registry', function() { + it('sets the locale stored in the registry', function () { instance.setLocale('foo'); assert.equal(instance._registry.locale, 'foo'); }); - it('returns the previous locale that was stored in the registry', function() { - var current = instance.getLocale(); + it('returns the previous locale that was stored in the registry', function () { + var current = instance.getLocale(); var previous = instance.setLocale(current + 'x'); assert.equal(previous, current); }); - describe('when called with a locale that differs from the current one', function() { - it('emits a "localechange" event', function(done) { - var handler = function() { done() }; + describe('when called with a locale that differs from the current one', function () { + it('emits a "localechange" event', function (done) { + var handler = function () { + done(); + }; instance.onLocaleChange(handler); instance.setLocale(instance.getLocale() + 'x'); instance.offLocaleChange(handler); }); }); - describe('when called with the current locale', function() { - it('does not emit a "localechange" event', function(done) { - var handler = function() { done('event was emitted'); }; + describe('when called with the current locale', function () { + it('does not emit a "localechange" event', function (done) { + var handler = function () { + done('event was emitted'); + }; instance.onLocaleChange(handler); instance.setLocale(instance.getLocale()); instance.offLocaleChange(handler); @@ -462,152 +880,176 @@ describe('translate', function() { }); }); - describe('#getFallbackLocale', function() { - it('is a function', function() { + describe('#getFallbackLocale', function () { + it('is a function', function () { assert.isFunction(instance.getFallbackLocale); }); - it('returns the fallback locale stored in the registry', function() { - assert.equal(instance.getFallbackLocale(), instance._registry.fallbackLocales); + it('returns the fallback locale stored in the registry', function () { + assert.equal( + instance.getFallbackLocale(), + instance._registry.fallbackLocales, + ); }); - it('returns an empty array by default', function() { + it('returns an empty array by default', function () { assert.deepEqual(instance.getFallbackLocale(), []); }); }); - describe('#setFallbackLocale', function() { - it('is a function', function() { + describe('#setFallbackLocale', function () { + it('is a function', function () { assert.isFunction(instance.setFallbackLocale); }); - it('sets the fallback locale stored in the registry', function() { + it('sets the fallback locale stored in the registry', function () { instance.setFallbackLocale('foo'); assert.deepEqual(instance._registry.fallbackLocales, ['foo']); }); - it('returns the previous fallback locale that was stored in the registry', function() { - var current = instance.getFallbackLocale(); + it('returns the previous fallback locale that was stored in the registry', function () { + var current = instance.getFallbackLocale(); var previous = instance.setFallbackLocale(current + 'x'); assert.equal(previous, current); }); }); - describe('#getAvailableLocales', function() { - it('is a function', function() { + describe('#getAvailableLocales', function () { + it('is a function', function () { assert.isFunction(instance.getAvailableLocales); }); - it('returns the locales of the registered translations by default', function() { - assert.deepEqual(instance.getAvailableLocales(), Object.keys(instance._registry.translations)); + it('returns the locales of the registered translations by default', function () { + assert.deepEqual( + instance.getAvailableLocales(), + Object.keys(instance._registry.translations), + ); }); }); - describe('#setAvailableLocales', function() { - it('is a function', function() { + describe('#setAvailableLocales', function () { + it('is a function', function () { assert.isFunction(instance.setAvailableLocales); }); - it('sets the locales available', function() { + it('sets the locales available', function () { instance.setAvailableLocales(['foo', 'bar']); assert.deepEqual(instance._registry.availableLocales, ['foo', 'bar']); }); - it('returns the previous available locales', function() { - var current = instance.getAvailableLocales(); + it('returns the previous available locales', function () { + var current = instance.getAvailableLocales(); var previous = instance.setAvailableLocales(current.concat('x')); assert.deepEqual(previous, current); }); }); - describe('#withLocale', function() { - it('is a function', function() { + describe('#withLocale', function () { + it('is a function', function () { assert.isFunction(instance.withLocale); }); - it('temporarily changes the current locale within the callback', function() { + it('temporarily changes the current locale within the callback', function () { var locale = instance.getLocale(); - instance.withLocale(locale + 'x', function() { + instance.withLocale(locale + 'x', function () { assert.equal(instance.getLocale(), locale + 'x'); }); assert.equal(instance.getLocale(), locale); }); - it('allows a custom callback context to be set', function() { - instance.withLocale('foo', function() { - assert.equal(this.bar, 'baz'); - }, { bar: 'baz' }) + it('allows a custom callback context to be set', function () { + instance.withLocale( + 'foo', + function () { + assert.equal(this.bar, 'baz'); + }, + { bar: 'baz' }, + ); }); - it('does not emit a "localechange" event', function(done) { - var handler = function() { done('event was emitted'); }; + it('does not emit a "localechange" event', function (done) { + var handler = function () { + done('event was emitted'); + }; instance.onLocaleChange(handler); - instance.withLocale(instance.getLocale() + 'x', function() {}); + instance.withLocale(instance.getLocale() + 'x', function () {}); instance.offLocaleChange(handler); setTimeout(done, 100); }); - it('returns the return value of the callback', function() { - var result = instance.withLocale('foo', function() { return 'bar'; }); + it('returns the return value of the callback', function () { + var result = instance.withLocale('foo', function () { + return 'bar'; + }); assert.equal(result, 'bar'); }); }); - describe('#withScope', function() { - it('is a function', function() { + describe('#withScope', function () { + it('is a function', function () { assert.isFunction(instance.withScope); }); - it('temporarily changes the current scope within the callback', function() { + it('temporarily changes the current scope within the callback', function () { var scope = instance._registry.scope; - instance.withScope(scope + 'x', function() { + instance.withScope(scope + 'x', function () { assert.equal(instance._registry.scope, scope + 'x'); }); assert.equal(instance._registry.scope, scope); }); - it('allows a custom callback context to be set', function() { - instance.withScope('foo', function() { - assert.equal(this.bar, 'baz'); - }, { bar: 'baz' }) + it('allows a custom callback context to be set', function () { + instance.withScope( + 'foo', + function () { + assert.equal(this.bar, 'baz'); + }, + { bar: 'baz' }, + ); }); - it('returns the return value of the callback', function() { - var result = instance.withScope('foo', function() { return 'bar'; }); + it('returns the return value of the callback', function () { + var result = instance.withScope('foo', function () { + return 'bar'; + }); assert.equal(result, 'bar'); }); }); - describe('#onLocaleChange', function() { - it('is a function', function() { + describe('#onLocaleChange', function () { + it('is a function', function () { assert.isFunction(instance.onLocaleChange); }); - it('is called when the locale changes', function(done) { - var handler = function() { done(); }; + it('is called when the locale changes', function (done) { + var handler = function () { + done(); + }; instance.onLocaleChange(handler); instance.setLocale(instance.getLocale() + 'x'); instance.offLocaleChange(handler); }); - it('is not called when the locale does not change', function(done) { - var handler = function() { done('function was called'); }; + it('is not called when the locale does not change', function (done) { + var handler = function () { + done('function was called'); + }; instance.onLocaleChange(handler); instance.setLocale(instance.getLocale()); instance.offLocaleChange(handler); setTimeout(done, 100); }); - describe('when called', function() { - it('exposes both the new and old locale as arguments', function(done) { + describe('when called', function () { + it('exposes both the new and old locale as arguments', function (done) { var oldLocale = instance.getLocale(); var newLocale = oldLocale + 'x'; - var handler = function(evt) { + var handler = function (evt) { assert.equal(evt.detail.locale, newLocale); assert.equal(evt.detail.previous, oldLocale); done(); @@ -620,22 +1062,29 @@ describe('translate', function() { }); // EventTarget does not have a native `setMaxListeners`. - describe.skip('when called more than 10 times', function() { - it('does not let Node issue a warning about a possible memory leak', function() { + describe.skip('when called more than 10 times', function () { + it('does not let Node issue a warning about a possible memory leak', function () { var oldConsoleError = console.error; - console.error = function(message) { + console.error = function (message) { if (/EventEmitter memory leak/.test(message)) { - assert.fail(null, null, 'Node issues a warning about a possible memory leak', null); + assert.fail( + null, + null, + 'Node issues a warning about a possible memory leak', + null, + ); } else { oldConsoleError.apply(console, arguments); } }; - var handlers = [], handler, i; + var handlers = [], + handler, + i; for (i = 0; i < 11; i++) { - handler = function() {}; + handler = function () {}; instance.onLocaleChange(handler); handlers.push(handler); } @@ -644,20 +1093,22 @@ describe('translate', function() { instance.offLocaleChange(handlers[i]); } - console.error = oldConsoleError + console.error = oldConsoleError; }); - }) + }); }); - describe('#offLocaleChange', function() { - it('is a function', function() { + describe('#offLocaleChange', function () { + it('is a function', function () { assert.isFunction(instance.offLocaleChange); }); - it('stops the emission of events to the handler', function(done) { + it('stops the emission of events to the handler', function (done) { var count = 0; - var handler = function() { count++; }; + var handler = function () { + count++; + }; instance.onLocaleChange(handler); instance.setLocale(instance.getLocale() + 'x'); @@ -665,27 +1116,31 @@ describe('translate', function() { instance.offLocaleChange(handler); instance.setLocale(instance.getLocale() + 'x'); - setTimeout(function() { + setTimeout(function () { assert.equal(count, 2, 'handler was called although deactivated'); done(); }, 100); }); }); - describe('#onTranslationNotFound', function() { - it('is a function', function() { + describe('#onTranslationNotFound', function () { + it('is a function', function () { assert.isFunction(instance.onTranslationNotFound); }); - it('is called when the translation is missing', function(done) { - var handler = function() { done(); }; + it('is called when the translation is missing', function (done) { + var handler = function () { + done(); + }; instance.onTranslationNotFound(handler); instance.translate('foo'); instance.offTranslationNotFound(handler); }); - it('is not called when a translation exists', function(done) { - var handler = function() { done('function was called'); }; + it('is not called when a translation exists', function (done) { + var handler = function () { + done('function was called'); + }; instance.registerTranslations('xx', { foo: 'bar' }); instance.onTranslationNotFound(handler); instance.translate('foo', { locale: 'xx', fallback: 'baz' }); @@ -693,9 +1148,9 @@ describe('translate', function() { setTimeout(done, 100); }); - describe('when called', function() { - it('exposes the current locale, key, fallback and scope as arguments', function(done) { - var handler = function(evt) { + describe('when called', function () { + it('exposes the current locale, key, fallback and scope as arguments', function (done) { + var handler = function (evt) { assert.equal('yy', evt.detail.locale); assert.equal('foo', evt.detail.key); assert.equal('bar', evt.detail.fallback); @@ -704,21 +1159,27 @@ describe('translate', function() { }; instance.onTranslationNotFound(handler); - instance.translate('foo', { locale: 'yy', fallback: 'bar', scope: 'zz' }); + instance.translate('foo', { + locale: 'yy', + fallback: 'bar', + scope: 'zz', + }); instance.offTranslationNotFound(handler); }); }); }); - describe('#offTranslationNotFound', function() { - it('is a function', function() { + describe('#offTranslationNotFound', function () { + it('is a function', function () { assert.isFunction(instance.offTranslationNotFound); }); - it('stops the emission of events to the handler', function(done) { + it('stops the emission of events to the handler', function (done) { var count = 0; - var handler = function() { count++; }; + var handler = function () { + count++; + }; instance.onTranslationNotFound(handler); instance.translate('foo', { fallback: 'bar' }); @@ -726,45 +1187,49 @@ describe('translate', function() { instance.offTranslationNotFound(handler); instance.translate('foo', { fallback: 'bar' }); - setTimeout(function() { + setTimeout(function () { assert.equal(count, 2, 'handler was called although deactivated'); done(); }, 100); }); }); - describe('#onError', function() { - it('is a function', function() { + describe('#onError', function () { + it('is a function', function () { assert.isFunction(instance.onError); }); - it('is called when the translation throws', function(done) { - var handler = function() { done(); }; + it('is called when the translation throws', function (done) { + var handler = function () { + done(); + }; instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); instance.onError(handler); instance.translate('hello'); instance.offError(handler); }); - it('is not called when a translation succeeds', function(done) { - var handler = function() { done('function was called'); }; + it('is not called when a translation succeeds', function (done) { + var handler = function () { + done('function was called'); + }; instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); instance.onError(handler); - instance.translate('hello', { name: 'Martin'}); + instance.translate('hello', { name: 'Martin' }); instance.offError(handler); setTimeout(done, 100); }); - it('still throws when not listening for an error', function() { + it('still throws when not listening for an error', function () { instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); - assert.throws(function() { + assert.throws(function () { instance.translate('hello'); }); }); - describe('when called', function() { - it('exposes the error, entry and values as arguments', function(done) { - var handler = function(evt) { + describe('when called', function () { + it('exposes the error, entry and values as arguments', function (done) { + var handler = function (evt) { assert.notEqual(undefined, evt.detail.error); assert.equal('Hello, %(name)s!', evt.detail.entry); assert.deepEqual({}, evt.detail.values); @@ -779,15 +1244,17 @@ describe('translate', function() { }); }); - describe('#offError', function() { - it('is a function', function() { + describe('#offError', function () { + it('is a function', function () { assert.isFunction(instance.offError); }); - it('stops the emission of events to the handler', function(done) { + it('stops the emission of events to the handler', function (done) { var count = 0; - var handler = function() { count++; }; + var handler = function () { + count++; + }; instance.registerTranslations('en', { hello: 'Hello, %(name)s!' }); instance.onError(handler); @@ -797,36 +1264,35 @@ describe('translate', function() { try { instance.translate('hello'); - } - catch (err) {} + } catch (err) {} - setTimeout(function() { + setTimeout(function () { assert.equal(count, 2, 'handler was called although deactivated'); done(); }, 100); }); }); - describe('#getSeparator', function() { - it('is a function', function() { + describe('#getSeparator', function () { + it('is a function', function () { assert.isFunction(instance.getSeparator); }); - it('returns the separator stored in the registry', function() { + it('returns the separator stored in the registry', function () { assert.equal(instance.getSeparator(), instance._registry.separator); }); - it('returns "." by default', function() { + it('returns "." by default', function () { assert.equal(instance.getSeparator(), '.'); }); }); - describe('#setSeparator', function() { - it('is a function', function() { + describe('#setSeparator', function () { + it('is a function', function () { assert.isFunction(instance.setSeparator); }); - it('sets the separator stored in the registry', function() { + it('sets the separator stored in the registry', function () { var prev = instance._registry.separator; instance.setSeparator('*'); @@ -835,34 +1301,34 @@ describe('translate', function() { instance._registry.separator = prev; }); - it('returns the previous separator that was stored in the registry', function() { - var current = instance.getSeparator(); + it('returns the previous separator that was stored in the registry', function () { + var current = instance.getSeparator(); var previous = instance.setSeparator(current + 'x'); assert.equal(previous, current); instance.setSeparator(current); }); }); - describe('#getInterpolate', function() { - it('is a function', function() { + describe('#getInterpolate', function () { + it('is a function', function () { assert.isFunction(instance.getInterpolate); }); - it('returns the setting stored in the registry', function() { + it('returns the setting stored in the registry', function () { assert.equal(instance.getInterpolate(), instance._registry.interpolate); }); - it('returns true by default', function() { + it('returns true by default', function () { assert.equal(instance.getInterpolate(), true); }); }); - describe('#setInterpolate', function() { - it('is a function', function() { + describe('#setInterpolate', function () { + it('is a function', function () { assert.isFunction(instance.setInterpolate); }); - it('sets the interpolate stored in the registry', function() { + it('sets the interpolate stored in the registry', function () { var prev = instance._registry.interpolate; instance.setInterpolate(true); @@ -871,35 +1337,38 @@ describe('translate', function() { instance._registry.interpolate = prev; }); - it('returns the previous interpolate that was stored in the registry', function() { - var current = instance.getInterpolate(); + it('returns the previous interpolate that was stored in the registry', function () { + var current = instance.getInterpolate(); var previous = instance.setInterpolate(true); assert.equal(previous, current); instance.setInterpolate(current); }); }); - describe('#getKeyTransformer', function() { - it('is a function', function() { + describe('#getKeyTransformer', function () { + it('is a function', function () { assert.isFunction(instance.getKeyTransformer); }); - it('returns the setting stored in the registry', function() { - assert.equal(instance.getKeyTransformer(), instance._registry.keyTransformer); + it('returns the setting stored in the registry', function () { + assert.equal( + instance.getKeyTransformer(), + instance._registry.keyTransformer, + ); }); }); - describe('#setKeyTransformer', function() { - var transformer = function(key, options) { + describe('#setKeyTransformer', function () { + var transformer = function (key, options) { assert.deepEqual({ locale: 'xx', bingo: 'bongo' }, options); return key.toLowerCase(); }; - it('is a function', function() { + it('is a function', function () { assert.isFunction(instance.setKeyTransformer); }); - it('sets the keyTransformer stored in the registry', function() { + it('sets the keyTransformer stored in the registry', function () { var prev = instance._registry.keyTransformer; instance.setKeyTransformer(transformer); @@ -908,17 +1377,20 @@ describe('translate', function() { instance._registry.keyTransformer = prev; }); - it('returns the previous keyTransformer that was stored in the registry', function() { - var current = instance.getKeyTransformer(); + it('returns the previous keyTransformer that was stored in the registry', function () { + var current = instance.getKeyTransformer(); var previous = instance.setKeyTransformer(transformer); assert.equal(previous, current); instance.setKeyTransformer(current); }); - it('uses the custom key transformer when translating', function() { + it('uses the custom key transformer when translating', function () { instance.registerTranslations('xx', { foo: 'bar' }); - var translation = instance.translate('FOO', { locale: 'xx', bingo: 'bongo' }); + var translation = instance.translate('FOO', { + locale: 'xx', + bingo: 'bongo', + }); assert.matches(translation, /missing translation/); instance.setKeyTransformer(transformer); @@ -927,26 +1399,29 @@ describe('translate', function() { }); }); - describe('#getMissingEntryGenerator', function() { - it('is a function', function() { + describe('#getMissingEntryGenerator', function () { + it('is a function', function () { assert.isFunction(instance.getMissingEntryGenerator); }); - it('returns the setting stored in the registry', function() { - assert.equal(instance.getMissingEntryGenerator(), instance._registry.generateMissingEntry); + it('returns the setting stored in the registry', function () { + assert.equal( + instance.getMissingEntryGenerator(), + instance._registry.generateMissingEntry, + ); }); }); - describe('#setMissingEntryGenerator', function() { - var generator = function(key) { + describe('#setMissingEntryGenerator', function () { + var generator = function (key) { return 'Ooops! Translation missing for ' + key + '!!!'; }; - it('is a function', function() { + it('is a function', function () { assert.isFunction(instance.setMissingEntryGenerator); }); - it('sets the generator stored in the registry', function() { + it('sets the generator stored in the registry', function () { var prev = instance._registry.generateMissingEntry; instance.setMissingEntryGenerator(generator); @@ -955,967 +1430,1181 @@ describe('translate', function() { instance._registry.generateMissingEntry = prev; }); - it('returns the previous generator that was stored in the registry', function() { - var current = instance.getMissingEntryGenerator(); + it('returns the previous generator that was stored in the registry', function () { + var current = instance.getMissingEntryGenerator(); var previous = instance.setMissingEntryGenerator(generator); assert.equal(previous, current); instance.setMissingEntryGenerator(current); }); - it('uses the custom generator when translating', function() { + it('uses the custom generator when translating', function () { instance.setMissingEntryGenerator(generator); var translation = instance.translate('d', { locale: 'a', scope: 'b.c' }); assert.equal('Ooops! Translation missing for a.b.c.d!!!', translation); }); }); - describe('#withSeparator', function() { - it('is a function', function() { + describe('#withSeparator', function () { + it('is a function', function () { assert.isFunction(instance.withSeparator); }); - it('temporarily changes the current separator within the callback', function() { + it('temporarily changes the current separator within the callback', function () { var separator = instance.getSeparator(); - instance.withSeparator(separator + 'x', function() { + instance.withSeparator(separator + 'x', function () { assert.equal(instance.getSeparator(), separator + 'x'); }); assert.equal(instance.getSeparator(), separator); }); - it('allows a custom callback context to be set', function() { - instance.withSeparator('foo', function() { - assert.equal(this.bar, 'baz'); - }, { bar: 'baz' }) + it('allows a custom callback context to be set', function () { + instance.withSeparator( + 'foo', + function () { + assert.equal(this.bar, 'baz'); + }, + { bar: 'baz' }, + ); }); - it('returns the return value of the callback', function() { - var result = instance.withSeparator('foo', function() { return 'bar'; }); + it('returns the return value of the callback', function () { + var result = instance.withSeparator('foo', function () { + return 'bar'; + }); assert.equal(result, 'bar'); }); }); - describe('#localize', function() { - before(function() { + describe('#localize', function () { + before(function () { instance.setLocale('en'); }); - it('is a function', function() { + it('is a function', function () { assert.isFunction(instance.localize); }); - it('does not mutate these options', function() { - var options = { locale: 'en', scope: ['foo1', 'foo2'], count: 3, bar: { baz: 'bum' } }; + it('does not mutate these options', function () { + var options = { + locale: 'en', + scope: ['foo1', 'foo2'], + count: 3, + bar: { baz: 'bum' }, + }; instance.localize(new Date(), options); - assert.deepEqual(options, { locale: 'en', scope: ['foo1', 'foo2'], count: 3, bar: { baz: 'bum' } }); + assert.deepEqual(options, { + locale: 'en', + scope: ['foo1', 'foo2'], + count: 3, + bar: { baz: 'bum' }, + }); }); - describe('when called without a date as first argument', function() { - it('throws an invalid argument error', function() { - assert.throws(function() { + describe('when called without a date as first argument', function () { + it('throws an invalid argument error', function () { + assert.throws(function () { instance.localize('foo'); }, /invalid argument/); }); }); - describe('when called with a date as first argument', function() { + describe('when called with a date as first argument', function () { var date = new time.Date('Thu Feb 6 2014 05:09:04 GMT+0100 (CET)'); date.setTimezone('America/Chicago'); - describe('without providing options as second argument', function() { - it('returns the default localization for that date', function() { + describe('without providing options as second argument', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date); assert.equal(result, 'Wed, 5 Feb 2014 22:09'); }); }); - describe('providing a `format` key in the options', function() { - describe('with format = "default"', function() { - it('returns the default localization for that date', function() { + describe('providing a `format` key in the options', function () { + describe('with format = "default"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { format: 'default' }); assert.equal(result, 'Wed, 5 Feb 2014 22:09'); }); }); - describe('with format = "short"', function() { - it('returns the short localization for that date', function() { + describe('with format = "short"', function () { + it('returns the short localization for that date', function () { var result = instance.localize(date, { format: 'short' }); assert.equal(result, '5 Feb 22:09'); }); }); - describe('with format = "long"', function() { - it('returns the long localization for that date', function() { + describe('with format = "long"', function () { + it('returns the long localization for that date', function () { var result = instance.localize(date, { format: 'long' }); - assert.equal(result, 'Wednesday, February 5th, 2014 22:09:04 -06:00'); + assert.equal( + result, + 'Wednesday, February 5th, 2014 22:09:04 -06:00', + ); }); }); - describe('with an unknown format', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown format', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { format: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing a `type` key in the options', function() { - describe('with type = "datetime"', function() { - it('returns the default localization for that date', function() { + describe('providing a `type` key in the options', function () { + describe('with type = "datetime"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { type: 'datetime' }); assert.equal(result, 'Wed, 5 Feb 2014 22:09'); }); }); - describe('with type = "date"', function() { - it('returns the date localization for that date', function() { + describe('with type = "date"', function () { + it('returns the date localization for that date', function () { var result = instance.localize(date, { type: 'date' }); assert.equal(result, 'Wed, 5 Feb 2014'); }); }); - describe('with type = "time"', function() { - it('returns the time localization for that date', function() { + describe('with type = "time"', function () { + it('returns the time localization for that date', function () { var result = instance.localize(date, { type: 'time' }); assert.equal(result, '22:09'); }); }); - describe('with an unknown type', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown type', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { type: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing both a `type` key and a `format` key in the options', function() { - describe('with type = "datetime" and format = "default"', function() { - it('returns the default localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'default' }); + describe('providing both a `type` key and a `format` key in the options', function () { + describe('with type = "datetime" and format = "default"', function () { + it('returns the default localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'default', + }); assert.equal(result, 'Wed, 5 Feb 2014 22:09'); }); }); - describe('with type = "datetime" and format = "short"', function() { - it('returns the short datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'short' }); + describe('with type = "datetime" and format = "short"', function () { + it('returns the short datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'short', + }); assert.equal(result, '5 Feb 22:09'); }); }); - describe('with type = "datetime" and format = "long"', function() { - it('returns the long datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'long' }); - assert.equal(result, 'Wednesday, February 5th, 2014 22:09:04 -06:00'); + describe('with type = "datetime" and format = "long"', function () { + it('returns the long datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'long', + }); + assert.equal( + result, + 'Wednesday, February 5th, 2014 22:09:04 -06:00', + ); }); }); - describe('with type = "time" and format = "default"', function() { - it('returns the default time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'default' }); + describe('with type = "time" and format = "default"', function () { + it('returns the default time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'default', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "short"', function() { - it('returns the short time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'short' }); + describe('with type = "time" and format = "short"', function () { + it('returns the short time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'short', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "long"', function() { - it('returns the long time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'long' }); + describe('with type = "time" and format = "long"', function () { + it('returns the long time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'long', + }); assert.equal(result, '22:09:04 -06:00'); }); }); - describe('with type = "date" and format = "default"', function() { - it('returns the default date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'default' }); + describe('with type = "date" and format = "default"', function () { + it('returns the default date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'default', + }); assert.equal(result, 'Wed, 5 Feb 2014'); }); }); - describe('with type = "date" and format = "short"', function() { - it('returns the short date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'short' }); + describe('with type = "date" and format = "short"', function () { + it('returns the short date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'short', + }); assert.equal(result, 'Feb 5'); }); }); - describe('with type = "date" and format = "long"', function() { - it('returns the long date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'long' }); + describe('with type = "date" and format = "long"', function () { + it('returns the long date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'long', + }); assert.equal(result, 'Wednesday, February 5th, 2014'); }); }); - describe('with unknown type and unknown format', function() { - it('returns a string containing "missing translation"', function() { - var result = instance.localize(date, { type: '__invalid__', format: '__invalid__' }); + describe('with unknown type and unknown format', function () { + it('returns a string containing "missing translation"', function () { + var result = instance.localize(date, { + type: '__invalid__', + format: '__invalid__', + }); assert.matches(result, /missing translation/); }); }); }); - describe('with locale set to "de"', function() { + describe('with locale set to "de"', function () { var prev; - beforeEach(function() { + beforeEach(function () { instance.registerTranslations('de', require('./locales/de')); prev = instance.setLocale('de'); }); - afterEach(function() { + afterEach(function () { instance.setLocale(prev); }); - describe('without providing options as second argument', function() { - it('returns the default localization for that date', function() { + describe('without providing options as second argument', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date); assert.equal(result, 'Mi, 5. Feb 2014, 22:09 Uhr'); }); }); - describe('providing a `format` key in the options', function() { - describe('with format = "default"', function() { - it('returns the default localization for that date', function() { + describe('providing a `format` key in the options', function () { + describe('with format = "default"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { format: 'default' }); assert.equal(result, 'Mi, 5. Feb 2014, 22:09 Uhr'); }); }); - describe('with format = "short"', function() { - it('returns the short localization for that date', function() { + describe('with format = "short"', function () { + it('returns the short localization for that date', function () { var result = instance.localize(date, { format: 'short' }); assert.equal(result, '05.02.14 22:09'); }); }); - describe('with format = "long"', function() { - it('returns the long localization for that date', function() { + describe('with format = "long"', function () { + it('returns the long localization for that date', function () { var result = instance.localize(date, { format: 'long' }); - assert.equal(result, 'Mittwoch, 5. Februar 2014, 22:09:04 -06:00'); + assert.equal( + result, + 'Mittwoch, 5. Februar 2014, 22:09:04 -06:00', + ); }); }); - describe('with an unknown format', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown format', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { format: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing a `type` key in the options', function() { - describe('with type = "datetime"', function() { - it('returns the default localization for that date', function() { + describe('providing a `type` key in the options', function () { + describe('with type = "datetime"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { type: 'datetime' }); assert.equal(result, 'Mi, 5. Feb 2014, 22:09 Uhr'); }); }); - describe('with type = "date"', function() { - it('returns the date localization for that date', function() { + describe('with type = "date"', function () { + it('returns the date localization for that date', function () { var result = instance.localize(date, { type: 'date' }); assert.equal(result, 'Mi, 5. Feb 2014'); }); }); - describe('with type = "time"', function() { - it('returns the time localization for that date', function() { + describe('with type = "time"', function () { + it('returns the time localization for that date', function () { var result = instance.localize(date, { type: 'time' }); assert.equal(result, '22:09 Uhr'); }); }); - describe('with an unknown type', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown type', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { type: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing both a `type` key and a `format` key in the options', function() { - describe('with type = "datetime" and format = "default"', function() { - it('returns the default localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'default' }); + describe('providing both a `type` key and a `format` key in the options', function () { + describe('with type = "datetime" and format = "default"', function () { + it('returns the default localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'default', + }); assert.equal(result, 'Mi, 5. Feb 2014, 22:09 Uhr'); }); }); - describe('with type = "datetime" and format = "short"', function() { - it('returns the short datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'short' }); + describe('with type = "datetime" and format = "short"', function () { + it('returns the short datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'short', + }); assert.equal(result, '05.02.14 22:09'); }); }); - describe('with type = "datetime" and format = "long"', function() { - it('returns the long datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'long' }); - assert.equal(result, 'Mittwoch, 5. Februar 2014, 22:09:04 -06:00'); + describe('with type = "datetime" and format = "long"', function () { + it('returns the long datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'long', + }); + assert.equal( + result, + 'Mittwoch, 5. Februar 2014, 22:09:04 -06:00', + ); }); }); - describe('with type = "time" and format = "default"', function() { - it('returns the default time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'default' }); + describe('with type = "time" and format = "default"', function () { + it('returns the default time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'default', + }); assert.equal(result, '22:09 Uhr'); }); }); - describe('with type = "time" and format = "short"', function() { - it('returns the short time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'short' }); + describe('with type = "time" and format = "short"', function () { + it('returns the short time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'short', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "long"', function() { - it('returns the long time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'long' }); + describe('with type = "time" and format = "long"', function () { + it('returns the long time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'long', + }); assert.equal(result, '22:09:04 -06:00'); }); }); - describe('with type = "date" and format = "default"', function() { - it('returns the default date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'default' }); + describe('with type = "date" and format = "default"', function () { + it('returns the default date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'default', + }); assert.equal(result, 'Mi, 5. Feb 2014'); }); }); - describe('with type = "date" and format = "short"', function() { - it('returns the short date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'short' }); + describe('with type = "date" and format = "short"', function () { + it('returns the short date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'short', + }); assert.equal(result, '05.02.14'); }); }); - describe('with type = "date" and format = "long"', function() { - it('returns the long date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'long' }); + describe('with type = "date" and format = "long"', function () { + it('returns the long date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'long', + }); assert.equal(result, 'Mittwoch, 5. Februar 2014'); }); }); - describe('with unknown type and unknown format', function() { - it('returns a string containing "missing translation"', function() { - var result = instance.localize(date, { type: '__invalid__', format: '__invalid__' }); + describe('with unknown type and unknown format', function () { + it('returns a string containing "missing translation"', function () { + var result = instance.localize(date, { + type: '__invalid__', + format: '__invalid__', + }); assert.matches(result, /missing translation/); }); }); }); }); - describe('with locale set to "nl"', function() { + describe('with locale set to "nl"', function () { var prev; - beforeEach(function() { + beforeEach(function () { instance.registerTranslations('nl', require('./locales/nl')); prev = instance.setLocale('nl'); }); - afterEach(function() { + afterEach(function () { instance.setLocale(prev); }); - describe('without providing options as second argument', function() { - it('returns the default localization for that date', function() { + describe('without providing options as second argument', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date); assert.equal(result, '5 feb 2014, 22.09'); }); }); - describe('providing a `format` key in the options', function() { - describe('with format = "default"', function() { - it('returns the default localization for that date', function() { + describe('providing a `format` key in the options', function () { + describe('with format = "default"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { format: 'default' }); assert.equal(result, '5 feb 2014, 22.09'); }); }); - describe('with format = "short"', function() { - it('returns the short localization for that date', function() { + describe('with format = "short"', function () { + it('returns the short localization for that date', function () { var result = instance.localize(date, { format: 'short' }); assert.equal(result, '2014-02-05 22.09'); }); }); - describe('with format = "long"', function() { - it('returns the long localization for that date', function() { + describe('with format = "long"', function () { + it('returns the long localization for that date', function () { var result = instance.localize(date, { format: 'long' }); assert.equal(result, 'woensdag 5 februari 2014, 22.09.04'); }); }); - describe('with an unknown format', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown format', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { format: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing a `type` key in the options', function() { - describe('with type = "datetime"', function() { - it('returns the default localization for that date', function() { + describe('providing a `type` key in the options', function () { + describe('with type = "datetime"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { type: 'datetime' }); assert.equal(result, '5 feb 2014, 22.09'); }); }); - describe('with type = "date"', function() { - it('returns the date localization for that date', function() { + describe('with type = "date"', function () { + it('returns the date localization for that date', function () { var result = instance.localize(date, { type: 'date' }); assert.equal(result, '5 feb 2014'); }); }); - describe('with type = "time"', function() { - it('returns the time localization for that date', function() { + describe('with type = "time"', function () { + it('returns the time localization for that date', function () { var result = instance.localize(date, { type: 'time' }); assert.equal(result, '22.09'); }); }); - describe('with an unknown type', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown type', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { type: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing both a `type` key and a `format` key in the options', function() { - describe('with type = "datetime" and format = "default"', function() { - it('returns the default localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'default' }); + describe('providing both a `type` key and a `format` key in the options', function () { + describe('with type = "datetime" and format = "default"', function () { + it('returns the default localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'default', + }); assert.equal(result, '5 feb 2014, 22.09'); }); }); - describe('with type = "datetime" and format = "short"', function() { - it('returns the short datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'short' }); + describe('with type = "datetime" and format = "short"', function () { + it('returns the short datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'short', + }); assert.equal(result, '2014-02-05 22.09'); }); }); - describe('with type = "datetime" and format = "long"', function() { - it('returns the long datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'long' }); + describe('with type = "datetime" and format = "long"', function () { + it('returns the long datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'long', + }); assert.equal(result, 'woensdag 5 februari 2014, 22.09.04'); }); }); - describe('with type = "time" and format = "default"', function() { - it('returns the default time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'default' }); + describe('with type = "time" and format = "default"', function () { + it('returns the default time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'default', + }); assert.equal(result, '22.09'); }); }); - describe('with type = "time" and format = "short"', function() { - it('returns the short time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'short' }); + describe('with type = "time" and format = "short"', function () { + it('returns the short time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'short', + }); assert.equal(result, '22.09'); }); }); - describe('with type = "time" and format = "long"', function() { - it('returns the long time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'long' }); + describe('with type = "time" and format = "long"', function () { + it('returns the long time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'long', + }); assert.equal(result, '22.09.04'); }); }); - describe('with type = "date" and format = "default"', function() { - it('returns the default date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'default' }); + describe('with type = "date" and format = "default"', function () { + it('returns the default date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'default', + }); assert.equal(result, '5 feb 2014'); }); }); - describe('with type = "date" and format = "short"', function() { - it('returns the short date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'short' }); + describe('with type = "date" and format = "short"', function () { + it('returns the short date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'short', + }); assert.equal(result, '2014-02-05'); }); }); - describe('with type = "date" and format = "long"', function() { - it('returns the long date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'long' }); + describe('with type = "date" and format = "long"', function () { + it('returns the long date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'long', + }); assert.equal(result, 'woensdag 5 februari 2014'); }); }); - describe('with unknown type and unknown format', function() { - it('returns a string containing "missing translation"', function() { - var result = instance.localize(date, { type: '__invalid__', format: '__invalid__' }); + describe('with unknown type and unknown format', function () { + it('returns a string containing "missing translation"', function () { + var result = instance.localize(date, { + type: '__invalid__', + format: '__invalid__', + }); assert.matches(result, /missing translation/); }); }); }); }); - describe('with locale set to "pt-br"', function() { + describe('with locale set to "pt-br"', function () { var prev; - beforeEach(function() { + beforeEach(function () { instance.registerTranslations('pt-br', require('./locales/pt-br')); prev = instance.setLocale('pt-br'); }); - afterEach(function() { + afterEach(function () { instance.setLocale(prev); }); - describe('without providing options as second argument', function() { - it('returns the default localization for that date', function() { + describe('without providing options as second argument', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date); assert.equal(result, 'Qua, 5 de Fev de 2014 às 22:09'); }); }); - describe('providing a `format` key in the options', function() { - describe('with format = "default"', function() { - it('returns the default localization for that date', function() { + describe('providing a `format` key in the options', function () { + describe('with format = "default"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { format: 'default' }); assert.equal(result, 'Qua, 5 de Fev de 2014 às 22:09'); }); }); - describe('with format = "short"', function() { - it('returns the short localization for that date', function() { + describe('with format = "short"', function () { + it('returns the short localization for that date', function () { var result = instance.localize(date, { format: 'short' }); assert.equal(result, '05/02/14 às 22:09'); }); }); - describe('with format = "long"', function() { - it('returns the long localization for that date', function() { + describe('with format = "long"', function () { + it('returns the long localization for that date', function () { var result = instance.localize(date, { format: 'long' }); - assert.equal(result, 'Quarta-feira, 5 de Fevereiro de 2014 às 22:09:04 -06:00'); + assert.equal( + result, + 'Quarta-feira, 5 de Fevereiro de 2014 às 22:09:04 -06:00', + ); }); }); - describe('with an unknown format', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown format', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { format: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing a `type` key in the options', function() { - describe('with type = "datetime"', function() { - it('returns the default localization for that date', function() { + describe('providing a `type` key in the options', function () { + describe('with type = "datetime"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { type: 'datetime' }); assert.equal(result, 'Qua, 5 de Fev de 2014 às 22:09'); }); }); - describe('with type = "date"', function() { - it('returns the date localization for that date', function() { + describe('with type = "date"', function () { + it('returns the date localization for that date', function () { var result = instance.localize(date, { type: 'date' }); assert.equal(result, 'Qua, 5 de Fev de 2014'); }); }); - describe('with type = "time"', function() { - it('returns the time localization for that date', function() { + describe('with type = "time"', function () { + it('returns the time localization for that date', function () { var result = instance.localize(date, { type: 'time' }); assert.equal(result, '22:09'); }); }); - describe('with an unknown type', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown type', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { type: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing both a `type` key and a `format` key in the options', function() { - describe('with type = "datetime" and format = "default"', function() { - it('returns the default localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'default' }); + describe('providing both a `type` key and a `format` key in the options', function () { + describe('with type = "datetime" and format = "default"', function () { + it('returns the default localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'default', + }); assert.equal(result, 'Qua, 5 de Fev de 2014 às 22:09'); }); }); - describe('with type = "datetime" and format = "short"', function() { - it('returns the short datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'short' }); + describe('with type = "datetime" and format = "short"', function () { + it('returns the short datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'short', + }); assert.equal(result, '05/02/14 às 22:09'); }); }); - describe('with type = "datetime" and format = "long"', function() { - it('returns the long datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'long' }); - assert.equal(result, 'Quarta-feira, 5 de Fevereiro de 2014 às 22:09:04 -06:00'); + describe('with type = "datetime" and format = "long"', function () { + it('returns the long datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'long', + }); + assert.equal( + result, + 'Quarta-feira, 5 de Fevereiro de 2014 às 22:09:04 -06:00', + ); }); }); - describe('with type = "time" and format = "default"', function() { - it('returns the default time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'default' }); + describe('with type = "time" and format = "default"', function () { + it('returns the default time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'default', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "short"', function() { - it('returns the short time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'short' }); + describe('with type = "time" and format = "short"', function () { + it('returns the short time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'short', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "long"', function() { - it('returns the long time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'long' }); + describe('with type = "time" and format = "long"', function () { + it('returns the long time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'long', + }); assert.equal(result, '22:09:04 -06:00'); }); }); - describe('with type = "date" and format = "default"', function() { - it('returns the default date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'default' }); + describe('with type = "date" and format = "default"', function () { + it('returns the default date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'default', + }); assert.equal(result, 'Qua, 5 de Fev de 2014'); }); }); - describe('with type = "date" and format = "short"', function() { - it('returns the short date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'short' }); + describe('with type = "date" and format = "short"', function () { + it('returns the short date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'short', + }); assert.equal(result, '05/02/14'); }); }); - describe('with type = "date" and format = "long"', function() { - it('returns the long date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'long' }); + describe('with type = "date" and format = "long"', function () { + it('returns the long date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'long', + }); assert.equal(result, 'Quarta-feira, 5 de Fevereiro de 2014'); }); }); - describe('with unknown type and unknown format', function() { - it('returns a string containing "missing translation"', function() { - var result = instance.localize(date, { type: '__invalid__', format: '__invalid__' }); + describe('with unknown type and unknown format', function () { + it('returns a string containing "missing translation"', function () { + var result = instance.localize(date, { + type: '__invalid__', + format: '__invalid__', + }); assert.matches(result, /missing translation/); }); }); }); }); - describe('with locale set to "cs"', function() { + describe('with locale set to "cs"', function () { var prev; - beforeEach(function() { + beforeEach(function () { instance.registerTranslations('cs', require('./locales/cs')); prev = instance.setLocale('cs'); }); - afterEach(function() { + afterEach(function () { instance.setLocale(prev); }); - describe('without providing options as second argument', function() { - it('returns the default localization for that date', function() { + describe('without providing options as second argument', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date); assert.equal(result, 'st 5. úno 2014, 22:09 hod.'); }); }); - describe('providing a `format` key in the options', function() { - describe('with format = "default"', function() { - it('returns the default localization for that date', function() { + describe('providing a `format` key in the options', function () { + describe('with format = "default"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { format: 'default' }); assert.equal(result, 'st 5. úno 2014, 22:09 hod.'); }); }); - describe('with format = "short"', function() { - it('returns the short localization for that date', function() { + describe('with format = "short"', function () { + it('returns the short localization for that date', function () { var result = instance.localize(date, { format: 'short' }); assert.equal(result, '5. 2. 14, 22:09'); }); }); - describe('with format = "long"', function() { - it('returns the long localization for that date', function() { + describe('with format = "long"', function () { + it('returns the long localization for that date', function () { var result = instance.localize(date, { format: 'long' }); assert.equal(result, 'středa 5. únor 2014, 22:09:04 -06:00'); }); }); - describe('with an unknown format', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown format', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { format: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing a `type` key in the options', function() { - describe('with type = "datetime"', function() { - it('returns the default localization for that date', function() { + describe('providing a `type` key in the options', function () { + describe('with type = "datetime"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { type: 'datetime' }); assert.equal(result, 'st 5. úno 2014, 22:09 hod.'); }); }); - describe('with type = "date"', function() { - it('returns the date localization for that date', function() { + describe('with type = "date"', function () { + it('returns the date localization for that date', function () { var result = instance.localize(date, { type: 'date' }); assert.equal(result, 'st 5. úno 2014'); }); }); - describe('with type = "time"', function() { - it('returns the time localization for that date', function() { + describe('with type = "time"', function () { + it('returns the time localization for that date', function () { var result = instance.localize(date, { type: 'time' }); assert.equal(result, '22:09 hod.'); }); }); - describe('with an unknown type', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown type', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { type: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing both a `type` key and a `format` key in the options', function() { - describe('with type = "datetime" and format = "default"', function() { - it('returns the default localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'default' }); + describe('providing both a `type` key and a `format` key in the options', function () { + describe('with type = "datetime" and format = "default"', function () { + it('returns the default localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'default', + }); assert.equal(result, 'st 5. úno 2014, 22:09 hod.'); }); }); - describe('with type = "datetime" and format = "short"', function() { - it('returns the short datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'short' }); + describe('with type = "datetime" and format = "short"', function () { + it('returns the short datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'short', + }); assert.equal(result, '5. 2. 14, 22:09'); }); }); - describe('with type = "datetime" and format = "long"', function() { - it('returns the long datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'long' }); + describe('with type = "datetime" and format = "long"', function () { + it('returns the long datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'long', + }); assert.equal(result, 'středa 5. únor 2014, 22:09:04 -06:00'); }); }); - describe('with type = "time" and format = "default"', function() { - it('returns the default time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'default' }); + describe('with type = "time" and format = "default"', function () { + it('returns the default time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'default', + }); assert.equal(result, '22:09 hod.'); }); }); - describe('with type = "time" and format = "short"', function() { - it('returns the short time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'short' }); + describe('with type = "time" and format = "short"', function () { + it('returns the short time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'short', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "long"', function() { - it('returns the long time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'long' }); + describe('with type = "time" and format = "long"', function () { + it('returns the long time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'long', + }); assert.equal(result, '22:09:04 -06:00'); }); }); - describe('with type = "date" and format = "default"', function() { - it('returns the default date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'default' }); + describe('with type = "date" and format = "default"', function () { + it('returns the default date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'default', + }); assert.equal(result, 'st 5. úno 2014'); }); }); - describe('with type = "date" and format = "short"', function() { - it('returns the short date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'short' }); + describe('with type = "date" and format = "short"', function () { + it('returns the short date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'short', + }); assert.equal(result, '5. 2. 14'); }); }); - describe('with type = "date" and format = "long"', function() { - it('returns the long date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'long' }); + describe('with type = "date" and format = "long"', function () { + it('returns the long date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'long', + }); assert.equal(result, 'středa 5. únor 2014'); }); }); - describe('with unknown type and unknown format', function() { - it('returns a string containing "missing translation"', function() { - var result = instance.localize(date, { type: '__invalid__', format: '__invalid__' }); + describe('with unknown type and unknown format', function () { + it('returns a string containing "missing translation"', function () { + var result = instance.localize(date, { + type: '__invalid__', + format: '__invalid__', + }); assert.matches(result, /missing translation/); }); }); }); }); - describe('with locale set to "sk"', function() { + describe('with locale set to "sk"', function () { var prev; - beforeEach(function() { + beforeEach(function () { instance.registerTranslations('sk', require('./locales/sk')); prev = instance.setLocale('sk'); }); - afterEach(function() { + afterEach(function () { instance.setLocale(prev); }); - describe('without providing options as second argument', function() { - it('returns the default localization for that date', function() { + describe('without providing options as second argument', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date); assert.equal(result, 'st 5. feb 2014, 22:09 hod.'); }); }); - describe('providing a `format` key in the options', function() { - describe('with format = "default"', function() { - it('returns the default localization for that date', function() { + describe('providing a `format` key in the options', function () { + describe('with format = "default"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { format: 'default' }); assert.equal(result, 'st 5. feb 2014, 22:09 hod.'); }); }); - describe('with format = "short"', function() { - it('returns the short localization for that date', function() { + describe('with format = "short"', function () { + it('returns the short localization for that date', function () { var result = instance.localize(date, { format: 'short' }); assert.equal(result, '5. 2. 14, 22:09'); }); }); - describe('with format = "long"', function() { - it('returns the long localization for that date', function() { + describe('with format = "long"', function () { + it('returns the long localization for that date', function () { var result = instance.localize(date, { format: 'long' }); assert.equal(result, 'streda 5. február 2014, 22:09:04 -06:00'); }); }); - describe('with an unknown format', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown format', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { format: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing a `type` key in the options', function() { - describe('with type = "datetime"', function() { - it('returns the default localization for that date', function() { + describe('providing a `type` key in the options', function () { + describe('with type = "datetime"', function () { + it('returns the default localization for that date', function () { var result = instance.localize(date, { type: 'datetime' }); assert.equal(result, 'st 5. feb 2014, 22:09 hod.'); }); }); - describe('with type = "date"', function() { - it('returns the date localization for that date', function() { + describe('with type = "date"', function () { + it('returns the date localization for that date', function () { var result = instance.localize(date, { type: 'date' }); assert.equal(result, 'st 5. feb 2014'); }); }); - describe('with type = "time"', function() { - it('returns the time localization for that date', function() { + describe('with type = "time"', function () { + it('returns the time localization for that date', function () { var result = instance.localize(date, { type: 'time' }); assert.equal(result, '22:09 hod.'); }); }); - describe('with an unknown type', function() { - it('returns a string containing "missing translation"', function() { + describe('with an unknown type', function () { + it('returns a string containing "missing translation"', function () { var result = instance.localize(date, { type: '__invalid__' }); assert.matches(result, /missing translation/); }); }); }); - describe('providing both a `type` key and a `format` key in the options', function() { - describe('with type = "datetime" and format = "default"', function() { - it('returns the default localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'default' }); + describe('providing both a `type` key and a `format` key in the options', function () { + describe('with type = "datetime" and format = "default"', function () { + it('returns the default localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'default', + }); assert.equal(result, 'st 5. feb 2014, 22:09 hod.'); }); }); - describe('with type = "datetime" and format = "short"', function() { - it('returns the short datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'short' }); + describe('with type = "datetime" and format = "short"', function () { + it('returns the short datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'short', + }); assert.equal(result, '5. 2. 14, 22:09'); }); }); - describe('with type = "datetime" and format = "long"', function() { - it('returns the long datetime localization for that date', function() { - var result = instance.localize(date, { type: 'datetime', format: 'long' }); + describe('with type = "datetime" and format = "long"', function () { + it('returns the long datetime localization for that date', function () { + var result = instance.localize(date, { + type: 'datetime', + format: 'long', + }); assert.equal(result, 'streda 5. február 2014, 22:09:04 -06:00'); }); }); - describe('with type = "time" and format = "default"', function() { - it('returns the default time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'default' }); + describe('with type = "time" and format = "default"', function () { + it('returns the default time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'default', + }); assert.equal(result, '22:09 hod.'); }); }); - describe('with type = "time" and format = "short"', function() { - it('returns the short time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'short' }); + describe('with type = "time" and format = "short"', function () { + it('returns the short time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'short', + }); assert.equal(result, '22:09'); }); }); - describe('with type = "time" and format = "long"', function() { - it('returns the long time localization for that date', function() { - var result = instance.localize(date, { type: 'time', format: 'long' }); + describe('with type = "time" and format = "long"', function () { + it('returns the long time localization for that date', function () { + var result = instance.localize(date, { + type: 'time', + format: 'long', + }); assert.equal(result, '22:09:04 -06:00'); }); }); - describe('with type = "date" and format = "default"', function() { - it('returns the default date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'default' }); + describe('with type = "date" and format = "default"', function () { + it('returns the default date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'default', + }); assert.equal(result, 'st 5. feb 2014'); }); }); - describe('with type = "date" and format = "short"', function() { - it('returns the short date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'short' }); + describe('with type = "date" and format = "short"', function () { + it('returns the short date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'short', + }); assert.equal(result, '5. 2. 14'); }); }); - describe('with type = "date" and format = "long"', function() { - it('returns the long date localization for that date', function() { - var result = instance.localize(date, { type: 'date', format: 'long' }); + describe('with type = "date" and format = "long"', function () { + it('returns the long date localization for that date', function () { + var result = instance.localize(date, { + type: 'date', + format: 'long', + }); assert.equal(result, 'streda 5. február 2014'); }); }); - describe('with unknown type and unknown format', function() { - it('returns a string containing "missing translation"', function() { - var result = instance.localize(date, { type: '__invalid__', format: '__invalid__' }); + describe('with unknown type and unknown format', function () { + it('returns a string containing "missing translation"', function () { + var result = instance.localize(date, { + type: '__invalid__', + format: '__invalid__', + }); assert.matches(result, /missing translation/); }); }); @@ -1924,23 +2613,23 @@ describe('translate', function() { }); }); - describe('#registerTranslations', function() { - it('is a function', function() { + describe('#registerTranslations', function () { + it('is a function', function () { assert.isFunction(instance.registerTranslations); }); - it('returns the passed arguments as an object structure', function() { + it('returns the passed arguments as an object structure', function () { var locale = 'foo'; - var data = { bar: { baz: 'bingo' } }; + var data = { bar: { baz: 'bingo' } }; var actual = instance.registerTranslations(locale, data); - var expected = { foo: { bar: { baz: 'bingo' }}}; + var expected = { foo: { bar: { baz: 'bingo' } } }; assert.deepEqual(actual, expected); }); - it('merges the passed arguments correctly into the registry', function() { + it('merges the passed arguments correctly into the registry', function () { instance._registry.translations = {}; instance.registerTranslations('foo', { bar: { baz: 'bingo' } }); @@ -1952,7 +2641,9 @@ describe('translate', function() { assert.deepEqual(instance._registry.translations, expected); instance.registerTranslations('foo', { bing: { bong: 'beng' } }); - var expected = { foo: { bar: { baz: 'bingo', bam: 'boo' }, bing: { bong: 'beng' } } }; + var expected = { + foo: { bar: { baz: 'bingo', bam: 'boo' }, bing: { bong: 'beng' } }, + }; assert.deepEqual(instance._registry.translations, expected); // clean up @@ -1961,46 +2652,79 @@ describe('translate', function() { }); }); - describe('#registerInterpolations', function() { - it('is a function', function() { + describe('#registerInterpolations', function () { + it('is a function', function () { assert.isFunction(instance.registerInterpolations); }); - it('merges the passed arguments correctly into the registry', function() { + it('merges the passed arguments correctly into the registry', function () { instance._registry.interpolations = {}; instance.registerInterpolations({ foo: 'yes', bar: 'no' }); - assert.deepEqual(instance._registry.interpolations, { foo: 'yes', bar: 'no' }); + assert.deepEqual(instance._registry.interpolations, { + foo: 'yes', + bar: 'no', + }); instance.registerInterpolations({ baz: 'hey' }); - assert.deepEqual(instance._registry.interpolations, { foo: 'yes', bar: 'no', baz: 'hey' }); + assert.deepEqual(instance._registry.interpolations, { + foo: 'yes', + bar: 'no', + baz: 'hey', + }); // clean up instance._registry.interpolations = {}; }); }); - describe('explicitly checking the examples of the README', function() { - it('passes all tests', function() { + describe('explicitly checking the examples of the README', function () { + it('passes all tests', function () { translate.registerTranslations('en', { damals: { about_x_hours_ago: { - one: 'about one hour ago', - other: 'about %(count)s hours ago' - } - } + one: 'about one hour ago', + other: 'about %(count)s hours ago', + }, + }, }); - assert.deepEqual(translate('damals'), { about_x_hours_ago: { one: 'about one hour ago', other: 'about %(count)s hours ago' } }); - - assert.equal(translate('damals.about_x_hours_ago.one'), 'about one hour ago'); - assert.equal(translate(['damals', 'about_x_hours_ago', 'one']), 'about one hour ago'); - assert.equal(translate(['damals', 'about_x_hours_ago.one']), 'about one hour ago'); - assert.equal(translate('about_x_hours_ago.one', { scope: 'damals' }), 'about one hour ago'); - assert.equal(translate('one', { scope: 'damals.about_x_hours_ago' }), 'about one hour ago'); - assert.equal(translate('one', { scope: ['damals', 'about_x_hours_ago'] }), 'about one hour ago'); + assert.deepEqual(translate('damals'), { + about_x_hours_ago: { + one: 'about one hour ago', + other: 'about %(count)s hours ago', + }, + }); - assert.equal(translate('damals.about_x_hours_ago.one', { separator: '*' }), 'missing translation: en*damals.about_x_hours_ago.one'); + assert.equal( + translate('damals.about_x_hours_ago.one'), + 'about one hour ago', + ); + assert.equal( + translate(['damals', 'about_x_hours_ago', 'one']), + 'about one hour ago', + ); + assert.equal( + translate(['damals', 'about_x_hours_ago.one']), + 'about one hour ago', + ); + assert.equal( + translate('about_x_hours_ago.one', { scope: 'damals' }), + 'about one hour ago', + ); + assert.equal( + translate('one', { scope: 'damals.about_x_hours_ago' }), + 'about one hour ago', + ); + assert.equal( + translate('one', { scope: ['damals', 'about_x_hours_ago'] }), + 'about one hour ago', + ); + + assert.equal( + translate('damals.about_x_hours_ago.one', { separator: '*' }), + 'missing translation: en*damals.about_x_hours_ago.one', + ); translate.registerTranslations('en', { foo: 'foo %(bar)s' }); @@ -2008,78 +2732,138 @@ describe('translate', function() { translate.registerTranslations('en', { x_items: { - zero: 'No items.', - one: 'One item.', - other: '%(count)s items.' - } + zero: 'No items.', + one: 'One item.', + other: '%(count)s items.', + }, }); - assert.equal(translate('x_items', { count: 0 }), 'No items.'); - assert.equal(translate('x_items', { count: 1 }), 'One item.'); + assert.equal(translate('x_items', { count: 0 }), 'No items.'); + assert.equal(translate('x_items', { count: 1 }), 'One item.'); assert.equal(translate('x_items', { count: 42 }), '42 items.'); assert.equal(translate('baz', { fallback: 'default' }), 'default'); translate.registerTranslations('de', require('./locales/de')); - translate.registerTranslations('de', JSON.parse('{"my_project": {"greeting": "Hallo, %(name)s!","x_items": {"one": "1 Stück", "other": "%(count)s Stücke"}}}')); - - assert.equal(translate.withLocale('de', function() { return translate('greeting', { scope: 'my_project', name: 'Martin' }); }), 'Hallo, Martin!'); - assert.equal(translate.withLocale('de', function() { return translate('x_items', { scope: 'my_project', count: 1 }); }), '1 Stück'); + translate.registerTranslations( + 'de', + JSON.parse( + '{"my_project": {"greeting": "Hallo, %(name)s!","x_items": {"one": "1 Stück", "other": "%(count)s Stücke"}}}', + ), + ); + + assert.equal( + translate.withLocale('de', function () { + return translate('greeting', { scope: 'my_project', name: 'Martin' }); + }), + 'Hallo, Martin!', + ); + assert.equal( + translate.withLocale('de', function () { + return translate('x_items', { scope: 'my_project', count: 1 }); + }), + '1 Stück', + ); var date = new time.Date('Fri Feb 21 2014 13:46:24 GMT+0100 (CET)'); date.setTimezone('Europe/Amsterdam'); - assert.equal(translate.localize(date) , 'Fri, 21 Feb 2014 13:46'); - assert.equal(translate.localize(date, { format: 'short' }) , '21 Feb 13:46'); - assert.equal(translate.localize(date, { format: 'long' }) , 'Friday, February 21st, 2014 13:46:24 +01:00'); - - assert.equal(translate.localize(date, { type: 'date' }) , 'Fri, 21 Feb 2014'); - assert.equal(translate.localize(date, { type: 'date', format: 'short' }) , 'Feb 21'); - assert.equal(translate.localize(date, { type: 'date', format: 'long' }) , 'Friday, February 21st, 2014'); - - assert.equal(translate.localize(date, { type: 'time' }) , '13:46'); - assert.equal(translate.localize(date, { type: 'time', format: 'short' }) , '13:46'); - assert.equal(translate.localize(date, { type: 'time', format: 'long' }) , '13:46:24 +01:00'); - - assert.equal(translate.localize(date, { locale: 'de' }) , 'Fr, 21. Feb 2014, 13:46 Uhr'); + assert.equal(translate.localize(date), 'Fri, 21 Feb 2014 13:46'); + assert.equal( + translate.localize(date, { format: 'short' }), + '21 Feb 13:46', + ); + assert.equal( + translate.localize(date, { format: 'long' }), + 'Friday, February 21st, 2014 13:46:24 +01:00', + ); + + assert.equal( + translate.localize(date, { type: 'date' }), + 'Fri, 21 Feb 2014', + ); + assert.equal( + translate.localize(date, { type: 'date', format: 'short' }), + 'Feb 21', + ); + assert.equal( + translate.localize(date, { type: 'date', format: 'long' }), + 'Friday, February 21st, 2014', + ); + + assert.equal(translate.localize(date, { type: 'time' }), '13:46'); + assert.equal( + translate.localize(date, { type: 'time', format: 'short' }), + '13:46', + ); + assert.equal( + translate.localize(date, { type: 'time', format: 'long' }), + '13:46:24 +01:00', + ); + + assert.equal( + translate.localize(date, { locale: 'de' }), + 'Fr, 21. Feb 2014, 13:46 Uhr', + ); translate.registerTranslations('en', { my_namespace: { - greeting: 'Welcome to %(app_name)s, %(visitor)s!' - } + greeting: 'Welcome to %(app_name)s, %(visitor)s!', + }, }); translate.registerInterpolations({ app_name: 'My Cool App' }); - assert.equal(translate('my_namespace.greeting', { visitor: 'Martin' }), 'Welcome to My Cool App, Martin!'); - assert.equal(translate('my_namespace.greeting', { visitor: 'Martin', app_name: 'The Foo App' }), 'Welcome to The Foo App, Martin!'); + assert.equal( + translate('my_namespace.greeting', { visitor: 'Martin' }), + 'Welcome to My Cool App, Martin!', + ); + assert.equal( + translate('my_namespace.greeting', { + visitor: 'Martin', + app_name: 'The Foo App', + }), + 'Welcome to The Foo App, Martin!', + ); }); }); }); - - - - /* Helper Functions */ -assert.isString = function(value, message) { - assert.equal(Object.prototype.toString.call(value), '[object String]', message || (value + ' is not a string')); +assert.isString = function (value, message) { + assert.equal( + Object.prototype.toString.call(value), + '[object String]', + message || value + ' is not a string', + ); }; -assert.isFunction = function(value, message) { - assert.equal(Object.prototype.toString.call(value), '[object Function]', message || (value + ' is not a function')); +assert.isFunction = function (value, message) { + assert.equal( + Object.prototype.toString.call(value), + '[object Function]', + message || value + ' is not a function', + ); }; -assert.isObject = function(value, message) { - assert.equal(Object.prototype.toString.call(value), '[object Object]', message || (value + ' is not an object')); +assert.isObject = function (value, message) { + assert.equal( + Object.prototype.toString.call(value), + '[object Object]', + message || value + ' is not an object', + ); }; -assert.isUndefined = function(value, message) { - assert.equal(Object.prototype.toString.call(value), '[object Undefined]', message || (value + ' is not undefined')); +assert.isUndefined = function (value, message) { + assert.equal( + Object.prototype.toString.call(value), + '[object Undefined]', + message || value + ' is not undefined', + ); }; -assert.matches = function(actual, expected, message) { +assert.matches = function (actual, expected, message) { if (!expected.test(actual)) { assert.fail(actual, expected, message, '!~'); } diff --git a/strftime.js b/strftime.js index d13b3db..091a874 100644 --- a/strftime.js +++ b/strftime.js @@ -7,59 +7,122 @@ function strftime(date, format, names) { names = names || dateNames; - return format.replace(/%([-_0]?.)/g, function(_, c) { + return format.replace(/%([-_0]?.)/g, function (_, c) { var padding = null; if (c.length == 2) { switch (c[0]) { - case '-': padding = ''; break; - case '_': padding = ' '; break; - case '0': padding = '0'; break; - default: return _; // should never reach this one + case '-': + padding = ''; + break; + case '_': + padding = ' '; + break; + case '0': + padding = '0'; + break; + default: + return _; // should never reach this one } c = c[1]; } switch (c) { - case 'A': return names.days[date.getDay()]; - case 'a': return names.abbreviated_days[date.getDay()]; - case 'B': return names.months[date.getMonth()]; - case 'b': return names.abbreviated_months[date.getMonth()]; - case 'C': return pad(Math.floor(date.getFullYear() / 100), padding); - case 'D': return strftime(date, '%m/%d/%y'); - case 'd': return pad(date.getDate(), padding); - case 'e': return date.getDate(); - case 'F': return strftime(date, '%Y-%m-%d'); - case 'H': return pad(date.getHours(), padding); - case 'h': return names.abbreviated_months[date.getMonth()]; - case 'I': return pad(hours12(date), padding); - case 'j': return pad(Math.ceil((date.getTime() - (new Date(date.getFullYear(), 0, 1)).getTime()) / (1000 * 60 * 60 * 24)), 3); - case 'k': return pad(date.getHours(), padding === null ? ' ' : padding); - case 'L': return pad(Math.floor(timestamp % 1000), 3); - case 'l': return pad(hours12(date), padding === null ? ' ' : padding); - case 'M': return pad(date.getMinutes(), padding); - case 'm': return pad(date.getMonth() + 1, padding); - case 'n': return '\n'; - case 'o': return String(date.getDate()) + ordinal(date.getDate()); - case 'P': return date.getHours() < 12 ? names.am.toLowerCase() : names.pm.toLowerCase(); - case 'p': return date.getHours() < 12 ? names.am.toUpperCase() : names.pm.toUpperCase(); - case 'R': return strftime(date, '%H:%M'); - case 'r': return strftime(date, '%I:%M:%S %p'); - case 'S': return pad(date.getSeconds(), padding); - case 's': return Math.floor(timestamp / 1000); - case 'T': return strftime(date, '%H:%M:%S'); - case 't': return '\t'; - case 'U': return pad(weekNumber(date, 'sunday'), padding); - case 'u': return date.getDay() === 0 ? 7 : date.getDay(); - case 'v': return strftime(date, '%e-%b-%Y'); - case 'W': return pad(weekNumber(date, 'monday'), padding); - case 'w': return date.getDay(); - case 'Y': return date.getFullYear(); - case 'y': var y = String(date.getFullYear()); return y.slice(y.length - 2); - case 'Z': var tzString = date.toString().match(/\((\w+)\)/); return tzString && tzString[1] || ''; - case 'z': var off = date.getTimezoneOffset(); return (off > 0 ? '-' : '+') + pad(Math.round(Math.abs(off / 60)), 2) + ':' + pad(off % 60, 2); - default: return c; + case 'A': + return names.days[date.getDay()]; + case 'a': + return names.abbreviated_days[date.getDay()]; + case 'B': + return names.months[date.getMonth()]; + case 'b': + return names.abbreviated_months[date.getMonth()]; + case 'C': + return pad(Math.floor(date.getFullYear() / 100), padding); + case 'D': + return strftime(date, '%m/%d/%y'); + case 'd': + return pad(date.getDate(), padding); + case 'e': + return date.getDate(); + case 'F': + return strftime(date, '%Y-%m-%d'); + case 'H': + return pad(date.getHours(), padding); + case 'h': + return names.abbreviated_months[date.getMonth()]; + case 'I': + return pad(hours12(date), padding); + case 'j': + return pad( + Math.ceil( + (date.getTime() - new Date(date.getFullYear(), 0, 1).getTime()) / + (1000 * 60 * 60 * 24), + ), + 3, + ); + case 'k': + return pad(date.getHours(), padding === null ? ' ' : padding); + case 'L': + return pad(Math.floor(timestamp % 1000), 3); + case 'l': + return pad(hours12(date), padding === null ? ' ' : padding); + case 'M': + return pad(date.getMinutes(), padding); + case 'm': + return pad(date.getMonth() + 1, padding); + case 'n': + return '\n'; + case 'o': + return String(date.getDate()) + ordinal(date.getDate()); + case 'P': + return date.getHours() < 12 + ? names.am.toLowerCase() + : names.pm.toLowerCase(); + case 'p': + return date.getHours() < 12 + ? names.am.toUpperCase() + : names.pm.toUpperCase(); + case 'R': + return strftime(date, '%H:%M'); + case 'r': + return strftime(date, '%I:%M:%S %p'); + case 'S': + return pad(date.getSeconds(), padding); + case 's': + return Math.floor(timestamp / 1000); + case 'T': + return strftime(date, '%H:%M:%S'); + case 't': + return '\t'; + case 'U': + return pad(weekNumber(date, 'sunday'), padding); + case 'u': + return date.getDay() === 0 ? 7 : date.getDay(); + case 'v': + return strftime(date, '%e-%b-%Y'); + case 'W': + return pad(weekNumber(date, 'monday'), padding); + case 'w': + return date.getDay(); + case 'Y': + return date.getFullYear(); + case 'y': + var y = String(date.getFullYear()); + return y.slice(y.length - 2); + case 'Z': + var tzString = date.toString().match(/\((\w+)\)/); + return (tzString && tzString[1]) || ''; + case 'z': + var off = date.getTimezoneOffset(); + return ( + (off > 0 ? '-' : '+') + + pad(Math.round(Math.abs(off / 60)), 2) + + ':' + + pad(off % 60, 2) + ); + default: + return c; } }); } @@ -100,16 +163,20 @@ function hours12(date) { } function ordinal(n) { - var i = n % 10, ii = n % 100; + var i = n % 10, + ii = n % 100; if ((ii >= 11 && ii <= 13) || i === 0 || i >= 4) { return 'th'; } switch (i) { - case 1: return 'st'; - case 2: return 'nd'; - case 3: return 'rd'; + case 1: + return 'st'; + case 2: + return 'nd'; + case 3: + return 'rd'; } } @@ -119,15 +186,15 @@ function weekNumber(date, firstWeekday) { var wday = date.getDay(); if (firstWeekday == 'monday') { - if (wday === 0) { // Sunday + if (wday === 0) { + // Sunday wday = 6; } else { wday--; } } - var - firstDayOfYear = new Date(date.getFullYear(), 0, 1), + var firstDayOfYear = new Date(date.getFullYear(), 0, 1), yday = (date - firstDayOfYear) / 86400000, weekNum = (yday + 7 - wday) / 7;