locales-detector
Advanced tools
Comparing version 1.1.1 to 2.0.0
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
## [2.0.0] 2018-11-10 | ||
### Changed | ||
- Migrate to typescript | ||
## [1.1.1] 2018-10-29 | ||
@@ -5,0 +9,0 @@ ### Fixed |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
// strict | ||
/** | ||
@@ -15,6 +7,4 @@ * Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
*/ | ||
var Detector = function Detector() { | ||
classCallCheck(this, Detector); | ||
}; | ||
class Detector {} | ||
module.exports = Detector; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
@@ -57,8 +22,4 @@ * Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
*/ | ||
var Detector = function Detector() { | ||
classCallCheck(this, Detector); | ||
}; | ||
class Detector {} | ||
// strict | ||
/** | ||
@@ -68,17 +29,13 @@ * With FromListDetector you can set locales by your own. | ||
var FromListDetector = function (_Detector) { | ||
inherits(FromListDetector, _Detector); | ||
class FromListDetector extends Detector { | ||
/** | ||
* @param {Array<string>} locales - list of locales | ||
*/ | ||
function FromListDetector(locales) { | ||
classCallCheck(this, FromListDetector); | ||
constructor(locales) { | ||
super(); | ||
var _this = possibleConstructorReturn(this, (FromListDetector.__proto__ || Object.getPrototypeOf(FromListDetector)).call(this)); | ||
_defineProperty(this, "locales", void 0); | ||
_this.locales = locales; | ||
return _this; | ||
this.locales = locales; | ||
} | ||
/** | ||
@@ -91,11 +48,8 @@ * Get list your of locales. | ||
createClass(FromListDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
return this.locales; | ||
} | ||
}]); | ||
return FromListDetector; | ||
}(Detector); | ||
getLocales() { | ||
return this.locales; | ||
} | ||
} | ||
module.exports = FromListDetector; |
@@ -5,56 +5,4 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var queryString = require('query-string'); | ||
var queryString = _interopDefault(require('query-string')); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
@@ -64,8 +12,19 @@ * Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
*/ | ||
var Detector = function Detector() { | ||
classCallCheck(this, Detector); | ||
}; | ||
class Detector {} | ||
// strict | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
@@ -75,57 +34,50 @@ * Navigator detector try load locales from browser navigator variable. | ||
var NavigatorDetector = function (_Detector) { | ||
inherits(NavigatorDetector, _Detector); | ||
class NavigatorDetector extends Detector { | ||
/** | ||
* On default languages are loaded from window.navigator | ||
* | ||
* @param {Navigator} navigator - browser navigator variable | ||
*/ | ||
constructor(navigator) { | ||
if (navigator === void 0) { | ||
navigator = window.navigator; | ||
} | ||
/** | ||
* On default languages are loaded from window.navigator | ||
* | ||
* @param {Navigator} navigator - browser navigator variable | ||
*/ | ||
function NavigatorDetector() { | ||
var navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.navigator; | ||
classCallCheck(this, NavigatorDetector); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (NavigatorDetector.__proto__ || Object.getPrototypeOf(NavigatorDetector)).call(this)); | ||
_defineProperty(this, "navigator", void 0); | ||
_this.navigator = navigator; | ||
return _this; | ||
} | ||
this.navigator = navigator; | ||
} | ||
/** | ||
* Get list of locales. | ||
* | ||
* @returns {Array<string>} list of locales from browser | ||
*/ | ||
/** | ||
* Get list of locales. | ||
* | ||
* @returns {Array<string>} list of locales from browser | ||
*/ | ||
getLocales() { | ||
// @ts-ignore | ||
const _this$navigator = this.navigator, | ||
languages = _this$navigator.languages, | ||
language = _this$navigator.language, | ||
userLanguage = _this$navigator.userLanguage; | ||
createClass(NavigatorDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
// $FlowFixMe | ||
var _navigator = this.navigator, | ||
languages = _navigator.languages, | ||
language = _navigator.language, | ||
userLanguage = _navigator.userLanguage; | ||
if (Array.isArray(languages)) { | ||
return languages; | ||
} | ||
if (language) { | ||
return [language]; | ||
} | ||
if (Array.isArray(languages)) { | ||
return languages; | ||
} | ||
if (userLanguage) { | ||
return [userLanguage]; | ||
} | ||
if (language) { | ||
return [language]; | ||
} | ||
return []; | ||
} | ||
if (userLanguage) { | ||
return [userLanguage]; | ||
} | ||
} | ||
return []; | ||
} | ||
}]); | ||
return NavigatorDetector; | ||
}(Detector); | ||
// strict | ||
/** | ||
@@ -135,45 +87,41 @@ * This detector load locales from GET parameters. The value should be locale separated list. | ||
var UrlDetector = function (_Detector) { | ||
inherits(UrlDetector, _Detector); | ||
class UrlDetector extends Detector { | ||
/** | ||
* Constructor | ||
* | ||
* @param {string} parameter - Which parameter to look for. | ||
* @param {Location} location - window.location is used on default | ||
*/ | ||
constructor(parameter, location) { | ||
if (location === void 0) { | ||
location = window.location; | ||
} | ||
/** | ||
* Constructor | ||
* | ||
* @param {string} parameter - Which parameter to look for. | ||
* @param {Location} location - window.location is used on default | ||
*/ | ||
function UrlDetector(parameter) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location; | ||
classCallCheck(this, UrlDetector); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (UrlDetector.__proto__ || Object.getPrototypeOf(UrlDetector)).call(this)); | ||
_defineProperty(this, "location", void 0); | ||
_this.location = location; | ||
_this.parameter = parameter; | ||
return _this; | ||
} | ||
_defineProperty(this, "parameter", void 0); | ||
/** | ||
* Get list of locales from browser url. | ||
* | ||
* @returns {Array<string>} list of locales | ||
*/ | ||
this.location = location; | ||
this.parameter = parameter; | ||
} | ||
/** | ||
* Get list of locales from browser url. | ||
* | ||
* @returns {Array<string>} list of locales | ||
*/ | ||
createClass(UrlDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
var locales = queryString.parse(this.location.search)[this.parameter] || null; | ||
getLocales() { | ||
const locales = queryString.parse(this.location.search)[this.parameter] || null; | ||
if (typeof locales === 'string') { | ||
return locales.split(','); | ||
} | ||
if (typeof locales === 'string') { | ||
return locales.split(','); | ||
} | ||
return []; | ||
} | ||
}]); | ||
return UrlDetector; | ||
}(Detector); | ||
return []; | ||
} | ||
// strict | ||
} | ||
@@ -184,17 +132,13 @@ /** | ||
var FromListDetector = function (_Detector) { | ||
inherits(FromListDetector, _Detector); | ||
class FromListDetector extends Detector { | ||
/** | ||
* @param {Array<string>} locales - list of locales | ||
*/ | ||
function FromListDetector(locales) { | ||
classCallCheck(this, FromListDetector); | ||
constructor(locales) { | ||
super(); | ||
var _this = possibleConstructorReturn(this, (FromListDetector.__proto__ || Object.getPrototypeOf(FromListDetector)).call(this)); | ||
_defineProperty(this, "locales", void 0); | ||
_this.locales = locales; | ||
return _this; | ||
this.locales = locales; | ||
} | ||
/** | ||
@@ -207,15 +151,11 @@ * Get list your of locales. | ||
createClass(FromListDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
return this.locales; | ||
} | ||
}]); | ||
return FromListDetector; | ||
}(Detector); | ||
getLocales() { | ||
return this.locales; | ||
} | ||
// strict | ||
} | ||
exports.Detector = Detector; | ||
exports.NavigatorDetector = NavigatorDetector; | ||
exports.UrlDetector = UrlDetector; | ||
exports.FromListDetector = FromListDetector; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
@@ -57,8 +22,4 @@ * Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
*/ | ||
var Detector = function Detector() { | ||
classCallCheck(this, Detector); | ||
}; | ||
class Detector {} | ||
// strict | ||
/** | ||
@@ -68,55 +29,50 @@ * Navigator detector try load locales from browser navigator variable. | ||
var NavigatorDetector = function (_Detector) { | ||
inherits(NavigatorDetector, _Detector); | ||
class NavigatorDetector extends Detector { | ||
/** | ||
* On default languages are loaded from window.navigator | ||
* | ||
* @param {Navigator} navigator - browser navigator variable | ||
*/ | ||
constructor(navigator) { | ||
if (navigator === void 0) { | ||
navigator = window.navigator; | ||
} | ||
/** | ||
* On default languages are loaded from window.navigator | ||
* | ||
* @param {Navigator} navigator - browser navigator variable | ||
*/ | ||
function NavigatorDetector() { | ||
var navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.navigator; | ||
classCallCheck(this, NavigatorDetector); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (NavigatorDetector.__proto__ || Object.getPrototypeOf(NavigatorDetector)).call(this)); | ||
_defineProperty(this, "navigator", void 0); | ||
_this.navigator = navigator; | ||
return _this; | ||
} | ||
this.navigator = navigator; | ||
} | ||
/** | ||
* Get list of locales. | ||
* | ||
* @returns {Array<string>} list of locales from browser | ||
*/ | ||
/** | ||
* Get list of locales. | ||
* | ||
* @returns {Array<string>} list of locales from browser | ||
*/ | ||
getLocales() { | ||
// @ts-ignore | ||
const _this$navigator = this.navigator, | ||
languages = _this$navigator.languages, | ||
language = _this$navigator.language, | ||
userLanguage = _this$navigator.userLanguage; | ||
createClass(NavigatorDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
// $FlowFixMe | ||
var _navigator = this.navigator, | ||
languages = _navigator.languages, | ||
language = _navigator.language, | ||
userLanguage = _navigator.userLanguage; | ||
if (Array.isArray(languages)) { | ||
return languages; | ||
} | ||
if (language) { | ||
return [language]; | ||
} | ||
if (Array.isArray(languages)) { | ||
return languages; | ||
} | ||
if (userLanguage) { | ||
return [userLanguage]; | ||
} | ||
if (language) { | ||
return [language]; | ||
} | ||
return []; | ||
} | ||
if (userLanguage) { | ||
return [userLanguage]; | ||
} | ||
} | ||
return []; | ||
} | ||
}]); | ||
return NavigatorDetector; | ||
}(Detector); | ||
module.exports = NavigatorDetector; |
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var queryString = require('query-string'); | ||
var queryString = _interopDefault(require('query-string')); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
@@ -61,8 +24,4 @@ * Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
*/ | ||
var Detector = function Detector() { | ||
classCallCheck(this, Detector); | ||
}; | ||
class Detector {} | ||
// strict | ||
/** | ||
@@ -72,44 +31,42 @@ * This detector load locales from GET parameters. The value should be locale separated list. | ||
var UrlDetector = function (_Detector) { | ||
inherits(UrlDetector, _Detector); | ||
class UrlDetector extends Detector { | ||
/** | ||
* Constructor | ||
* | ||
* @param {string} parameter - Which parameter to look for. | ||
* @param {Location} location - window.location is used on default | ||
*/ | ||
constructor(parameter, location) { | ||
if (location === void 0) { | ||
location = window.location; | ||
} | ||
/** | ||
* Constructor | ||
* | ||
* @param {string} parameter - Which parameter to look for. | ||
* @param {Location} location - window.location is used on default | ||
*/ | ||
function UrlDetector(parameter) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location; | ||
classCallCheck(this, UrlDetector); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (UrlDetector.__proto__ || Object.getPrototypeOf(UrlDetector)).call(this)); | ||
_defineProperty(this, "location", void 0); | ||
_this.location = location; | ||
_this.parameter = parameter; | ||
return _this; | ||
} | ||
_defineProperty(this, "parameter", void 0); | ||
/** | ||
* Get list of locales from browser url. | ||
* | ||
* @returns {Array<string>} list of locales | ||
*/ | ||
this.location = location; | ||
this.parameter = parameter; | ||
} | ||
/** | ||
* Get list of locales from browser url. | ||
* | ||
* @returns {Array<string>} list of locales | ||
*/ | ||
createClass(UrlDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
var locales = queryString.parse(this.location.search)[this.parameter] || null; | ||
getLocales() { | ||
const locales = queryString.parse(this.location.search)[this.parameter] || null; | ||
if (typeof locales === 'string') { | ||
return locales.split(','); | ||
} | ||
if (typeof locales === 'string') { | ||
return locales.split(','); | ||
} | ||
return []; | ||
} | ||
}]); | ||
return UrlDetector; | ||
}(Detector); | ||
return []; | ||
} | ||
} | ||
module.exports = UrlDetector; |
@@ -5,561 +5,436 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var queryString = require('query-string'); | ||
var queryString = _interopDefault(require('query-string')); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var _extends = Object.assign || function (target) { | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
}; | ||
} | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
class LocaleResolver { | ||
constructor(detectors, transformers) { | ||
if (transformers === void 0) { | ||
transformers = []; | ||
} | ||
_defineProperty(this, "locales", void 0); | ||
_defineProperty(this, "transformers", void 0); | ||
this.locales = detectors.map(detector => { | ||
return detector.getLocales(); | ||
}).reduce((a, b) => a.concat(b), []); | ||
this.transformers = transformers; | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
getLocales(transformers) { | ||
if (transformers === void 0) { | ||
transformers = []; | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
let locales = this.locales; | ||
[...this.transformers, ...transformers].forEach(transformer => { | ||
locales = transformer.transform(locales); | ||
}); | ||
return locales.filter((value, index, array) => { | ||
return array.indexOf(value) === index; | ||
}); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
} | ||
var toConsumableArray = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} else { | ||
return Array.from(arr); | ||
} | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
* Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
* Get locales method always return array of locales. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Detector {} | ||
// strict | ||
/** | ||
* This transformers create fallbacks from complex locales to simpliest. | ||
* 'en-US-east' => ['en-US-east', 'en-US', 'en'] | ||
* 'es-ES' => ['es-ES', 'es'] | ||
* Navigator detector try load locales from browser navigator variable. | ||
*/ | ||
var FallbacksTransformer = function (_Transformer) { | ||
inherits(FallbacksTransformer, _Transformer); | ||
function FallbacksTransformer() { | ||
classCallCheck(this, FallbacksTransformer); | ||
return possibleConstructorReturn(this, (FallbacksTransformer.__proto__ || Object.getPrototypeOf(FallbacksTransformer)).apply(this, arguments)); | ||
class NavigatorDetector extends Detector { | ||
/** | ||
* On default languages are loaded from window.navigator | ||
* | ||
* @param {Navigator} navigator - browser navigator variable | ||
*/ | ||
constructor(navigator) { | ||
if (navigator === void 0) { | ||
navigator = window.navigator; | ||
} | ||
createClass(FallbacksTransformer, [{ | ||
key: 'transform', | ||
super(); | ||
/** | ||
* Add fallbacks to locales. Locales should be in IETF language tag format. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with fallbacks | ||
*/ | ||
value: function transform(locales) { | ||
return locales.map(function (locale) { | ||
var splitedLocale = locale.split('-'); | ||
return splitedLocale.map(function (value, index) { | ||
var localeGenerator = []; | ||
for (var i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, []); | ||
} | ||
}]); | ||
return FallbacksTransformer; | ||
}(Transformer); | ||
_defineProperty(this, "navigator", void 0); | ||
// strict | ||
this.navigator = navigator; | ||
} | ||
/** | ||
* Get list of locales. | ||
* | ||
* @returns {Array<string>} list of locales from browser | ||
*/ | ||
/** | ||
* This transformer convert locales to standard IETF language tag. | ||
*/ | ||
var IETFTransformer = function (_Transformer) { | ||
inherits(IETFTransformer, _Transformer); | ||
getLocales() { | ||
// @ts-ignore | ||
const _this$navigator = this.navigator, | ||
languages = _this$navigator.languages, | ||
language = _this$navigator.language, | ||
userLanguage = _this$navigator.userLanguage; | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} localeSeparator - how is locales parts separated | ||
*/ | ||
function IETFTransformer() { | ||
var localeSeparator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '-'; | ||
classCallCheck(this, IETFTransformer); | ||
if (Array.isArray(languages)) { | ||
return languages; | ||
} | ||
var _this = possibleConstructorReturn(this, (IETFTransformer.__proto__ || Object.getPrototypeOf(IETFTransformer)).call(this)); | ||
if (language) { | ||
return [language]; | ||
} | ||
_this.localeSeparator = localeSeparator; | ||
return _this; | ||
if (userLanguage) { | ||
return [userLanguage]; | ||
} | ||
/** | ||
* Convert locales to standard IETF language tag. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} list of transformed locales | ||
*/ | ||
return []; | ||
} | ||
} | ||
createClass(IETFTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
/** | ||
* This detector load locales from GET parameters. The value should be locale separated list. | ||
*/ | ||
return locales.map(function (locale) { | ||
return locale.split(_this2.localeSeparator).map(function (value, index) { | ||
return index === 1 ? value.toUpperCase() : value.toLowerCase(); | ||
}).join('-'); | ||
}); | ||
} | ||
}]); | ||
return IETFTransformer; | ||
}(Transformer); | ||
class UrlDetector extends Detector { | ||
/** | ||
* Constructor | ||
* | ||
* @param {string} parameter - Which parameter to look for. | ||
* @param {Location} location - window.location is used on default | ||
*/ | ||
constructor(parameter, location) { | ||
if (location === void 0) { | ||
location = window.location; | ||
} | ||
// strict | ||
super(); | ||
var defaultConvertTable = { | ||
'es-XL': 'es-419', | ||
hindi: 'hi', | ||
indonesian: 'id', | ||
english: 'en' | ||
}; | ||
_defineProperty(this, "location", void 0); | ||
/** | ||
* It convert invalid locales. | ||
*/ | ||
_defineProperty(this, "parameter", void 0); | ||
var InvalidLocalesTransformer = function (_Transformer) { | ||
inherits(InvalidLocalesTransformer, _Transformer); | ||
this.location = location; | ||
this.parameter = parameter; | ||
} | ||
/** | ||
* Get list of locales from browser url. | ||
* | ||
* @returns {Array<string>} list of locales | ||
*/ | ||
/** | ||
* Constructor. | ||
* | ||
* @param {SimpleObject} convertTable - simple convert table | ||
*/ | ||
function InvalidLocalesTransformer() { | ||
var convertTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, InvalidLocalesTransformer); | ||
var _this = possibleConstructorReturn(this, (InvalidLocalesTransformer.__proto__ || Object.getPrototypeOf(InvalidLocalesTransformer)).call(this)); | ||
getLocales() { | ||
const locales = queryString.parse(this.location.search)[this.parameter] || null; | ||
_this.convertTable = _extends({}, defaultConvertTable, convertTable); | ||
return _this; | ||
if (typeof locales === 'string') { | ||
return locales.split(','); | ||
} | ||
/** | ||
* Transform invalid languages. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} cleaned list of locales | ||
*/ | ||
return []; | ||
} | ||
} | ||
createClass(InvalidLocalesTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
return locales.map(function (locale) { | ||
return _this2.convertTable[locale] || locale; | ||
}); | ||
} | ||
}]); | ||
return InvalidLocalesTransformer; | ||
}(Transformer); | ||
// strict | ||
/** | ||
* Transform all locales to languages only with region variant. | ||
* With FromListDetector you can set locales by your own. | ||
*/ | ||
var LanguageOnlyTransformer = function (_Transformer) { | ||
inherits(LanguageOnlyTransformer, _Transformer); | ||
class FromListDetector extends Detector { | ||
/** | ||
* @param {Array<string>} locales - list of locales | ||
*/ | ||
constructor(locales) { | ||
super(); | ||
function LanguageOnlyTransformer() { | ||
classCallCheck(this, LanguageOnlyTransformer); | ||
return possibleConstructorReturn(this, (LanguageOnlyTransformer.__proto__ || Object.getPrototypeOf(LanguageOnlyTransformer)).apply(this, arguments)); | ||
} | ||
_defineProperty(this, "locales", void 0); | ||
createClass(LanguageOnlyTransformer, [{ | ||
key: 'transform', | ||
this.locales = locales; | ||
} | ||
/** | ||
* Get list your of locales. | ||
* | ||
* @returns {Array<string>} list of your locales | ||
*/ | ||
/** | ||
* Transform locales to languages only with region variant. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} - list of languages | ||
*/ | ||
value: function transform(locales) { | ||
return locales.map(function (locale) { | ||
return locale.split('-')[0]; | ||
}); | ||
} | ||
}]); | ||
return LanguageOnlyTransformer; | ||
}(Transformer); | ||
// strict | ||
getLocales() { | ||
return this.locales; | ||
} | ||
/** | ||
* This transformer allows you to append default locale. | ||
*/ | ||
} | ||
var DefaultLocaleTransformer = function (_Transformer) { | ||
inherits(DefaultLocaleTransformer, _Transformer); | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} defaultLocale - set default locale | ||
*/ | ||
function DefaultLocaleTransformer(defaultLocale) { | ||
classCallCheck(this, DefaultLocaleTransformer); | ||
var _this = possibleConstructorReturn(this, (DefaultLocaleTransformer.__proto__ || Object.getPrototypeOf(DefaultLocaleTransformer)).call(this)); | ||
var index = ({ | ||
Detector: Detector, | ||
NavigatorDetector: NavigatorDetector, | ||
UrlDetector: UrlDetector, | ||
FromListDetector: FromListDetector | ||
}); | ||
_this.defaultLocale = defaultLocale; | ||
return _this; | ||
} | ||
/** | ||
* Add default locale to end of array | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with default | ||
*/ | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
class Transformer {} | ||
/** | ||
* This transformers create fallbacks from complex locales to simpliest. | ||
* 'en-US-east' => ['en-US-east', 'en-US', 'en'] | ||
* 'es-ES' => ['es-ES', 'es'] | ||
*/ | ||
createClass(DefaultLocaleTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var transformedLocales = [].concat(toConsumableArray(locales.map(function (locale) { | ||
var splitedLocale = locale.split('-'); | ||
return splitedLocale.map(function (value, index) { | ||
var localeGenerator = []; | ||
for (var i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, [])), [this.defaultLocale]); | ||
class FallbacksTransformer extends Transformer { | ||
/** | ||
* Add fallbacks to locales. Locales should be in IETF language tag format. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with fallbacks | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
const splitedLocale = locale.split('-'); | ||
return splitedLocale.map((value, index) => { | ||
const localeGenerator = []; | ||
// slice locales that comes after default locale | ||
return transformedLocales.slice(0, transformedLocales.indexOf(this.defaultLocale) + 1); | ||
for (let i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
}]); | ||
return DefaultLocaleTransformer; | ||
}(Transformer); | ||
// strict | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce((a, b) => a.concat(b), []); | ||
} | ||
} | ||
/** | ||
* This transformer allows you to filter locales | ||
* This transformer convert locales to standard IETF language tag. | ||
*/ | ||
var AllowOnlyTransformer = function (_Transformer) { | ||
inherits(AllowOnlyTransformer, _Transformer); | ||
/** | ||
* Constructor. | ||
* | ||
* @param {Array<string>} allowedLocales - list of allowed locales | ||
*/ | ||
function AllowOnlyTransformer(allowedLocales) { | ||
classCallCheck(this, AllowOnlyTransformer); | ||
var _this = possibleConstructorReturn(this, (AllowOnlyTransformer.__proto__ || Object.getPrototypeOf(AllowOnlyTransformer)).call(this)); | ||
_this.allowedLocales = allowedLocales; | ||
return _this; | ||
class IETFTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} localeSeparator - how is locales parts separated | ||
*/ | ||
constructor(localeSeparator) { | ||
if (localeSeparator === void 0) { | ||
localeSeparator = '-'; | ||
} | ||
/** | ||
* Return only allowed locales. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} allowed locales | ||
*/ | ||
super(); | ||
_defineProperty(this, "localeSeparator", void 0); | ||
createClass(AllowOnlyTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
this.localeSeparator = localeSeparator; | ||
} | ||
/** | ||
* Convert locales to standard IETF language tag. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} list of transformed locales | ||
*/ | ||
return locales.filter(function (locale) { | ||
return _this2.allowedLocales.indexOf(locale) >= 0; | ||
}); | ||
} | ||
}]); | ||
return AllowOnlyTransformer; | ||
}(Transformer); | ||
// strict | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return locale.split(this.localeSeparator).map((value, index) => { | ||
return index === 1 ? value.toUpperCase() : value.toLowerCase(); | ||
}).join('-'); | ||
}); | ||
} | ||
// strict | ||
} | ||
/** | ||
* Detector classes obtain list of user's locales. It can be from browser, url, cookies, storage, ... whatever you want | ||
* Get locales method always return array of locales. | ||
*/ | ||
var Detector = function Detector() { | ||
classCallCheck(this, Detector); | ||
const defaultConvertTable = { | ||
'es-XL': 'es-419', | ||
hindi: 'hi', | ||
indonesian: 'id', | ||
english: 'en' | ||
}; | ||
// strict | ||
/** | ||
* Navigator detector try load locales from browser navigator variable. | ||
* It convert invalid locales. | ||
*/ | ||
var NavigatorDetector = function (_Detector) { | ||
inherits(NavigatorDetector, _Detector); | ||
/** | ||
* On default languages are loaded from window.navigator | ||
* | ||
* @param {Navigator} navigator - browser navigator variable | ||
*/ | ||
function NavigatorDetector() { | ||
var navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.navigator; | ||
classCallCheck(this, NavigatorDetector); | ||
var _this = possibleConstructorReturn(this, (NavigatorDetector.__proto__ || Object.getPrototypeOf(NavigatorDetector)).call(this)); | ||
_this.navigator = navigator; | ||
return _this; | ||
class InvalidLocalesTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {SimpleObject} convertTable - simple convert table | ||
*/ | ||
constructor(convertTable) { | ||
if (convertTable === void 0) { | ||
convertTable = {}; | ||
} | ||
/** | ||
* Get list of locales. | ||
* | ||
* @returns {Array<string>} list of locales from browser | ||
*/ | ||
super(); | ||
_defineProperty(this, "convertTable", void 0); | ||
createClass(NavigatorDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
// $FlowFixMe | ||
var _navigator = this.navigator, | ||
languages = _navigator.languages, | ||
language = _navigator.language, | ||
userLanguage = _navigator.userLanguage; | ||
this.convertTable = _objectSpread({}, defaultConvertTable, convertTable); | ||
} | ||
/** | ||
* Transform invalid languages. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} cleaned list of locales | ||
*/ | ||
if (Array.isArray(languages)) { | ||
return languages; | ||
} | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return this.convertTable[locale] || locale; | ||
}); | ||
} | ||
if (language) { | ||
return [language]; | ||
} | ||
} | ||
if (userLanguage) { | ||
return [userLanguage]; | ||
} | ||
/** | ||
* Transform all locales to languages only with region variant. | ||
*/ | ||
return []; | ||
} | ||
}]); | ||
return NavigatorDetector; | ||
}(Detector); | ||
class LanguageOnlyTransformer extends Transformer { | ||
/** | ||
* Transform locales to languages only with region variant. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} - list of languages | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return locale.split('-')[0]; | ||
}); | ||
} | ||
// strict | ||
} | ||
/** | ||
* This detector load locales from GET parameters. The value should be locale separated list. | ||
* This transformer allows you to append default locale. | ||
*/ | ||
var UrlDetector = function (_Detector) { | ||
inherits(UrlDetector, _Detector); | ||
class DefaultLocaleTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} defaultLocale - set default locale | ||
*/ | ||
constructor(defaultLocale) { | ||
super(); | ||
/** | ||
* Constructor | ||
* | ||
* @param {string} parameter - Which parameter to look for. | ||
* @param {Location} location - window.location is used on default | ||
*/ | ||
function UrlDetector(parameter) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location; | ||
classCallCheck(this, UrlDetector); | ||
_defineProperty(this, "defaultLocale", void 0); | ||
var _this = possibleConstructorReturn(this, (UrlDetector.__proto__ || Object.getPrototypeOf(UrlDetector)).call(this)); | ||
this.defaultLocale = defaultLocale; | ||
} | ||
/** | ||
* Add default locale to end of array | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with default | ||
*/ | ||
_this.location = location; | ||
_this.parameter = parameter; | ||
return _this; | ||
} | ||
/** | ||
* Get list of locales from browser url. | ||
* | ||
* @returns {Array<string>} list of locales | ||
*/ | ||
transform(locales) { | ||
const transformedLocales = [...locales.map(locale => { | ||
const splitedLocale = locale.split('-'); | ||
return splitedLocale.map((value, index) => { | ||
const localeGenerator = []; | ||
for (let i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
createClass(UrlDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
var locales = queryString.parse(this.location.search)[this.parameter] || null; | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce((a, b) => a.concat(b), []), ...[this.defaultLocale]]; // slice locales that comes after default locale | ||
if (typeof locales === 'string') { | ||
return locales.split(','); | ||
} | ||
return transformedLocales.slice(0, transformedLocales.indexOf(this.defaultLocale) + 1); | ||
} | ||
return []; | ||
} | ||
}]); | ||
return UrlDetector; | ||
}(Detector); | ||
} | ||
// strict | ||
/** | ||
* With FromListDetector you can set locales by your own. | ||
* This transformer allows you to filter locales | ||
*/ | ||
var FromListDetector = function (_Detector) { | ||
inherits(FromListDetector, _Detector); | ||
class AllowOnlyTransformer extends Transformer { | ||
/** | ||
* @param {Array<string>} locales - list of locales | ||
* Constructor. | ||
* | ||
* @param {Array<string>} allowedLocales - list of allowed locales | ||
*/ | ||
function FromListDetector(locales) { | ||
classCallCheck(this, FromListDetector); | ||
constructor(allowedLocales) { | ||
super(); | ||
var _this = possibleConstructorReturn(this, (FromListDetector.__proto__ || Object.getPrototypeOf(FromListDetector)).call(this)); | ||
_defineProperty(this, "allowedLocales", void 0); | ||
_this.locales = locales; | ||
return _this; | ||
this.allowedLocales = allowedLocales; | ||
} | ||
/** | ||
* Get list your of locales. | ||
* Return only allowed locales. | ||
* | ||
* @returns {Array<string>} list of your locales | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} allowed locales | ||
*/ | ||
createClass(FromListDetector, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
return this.locales; | ||
} | ||
}]); | ||
return FromListDetector; | ||
}(Detector); | ||
transform(locales) { | ||
return locales.filter(locale => { | ||
return this.allowedLocales.indexOf(locale) >= 0; | ||
}); | ||
} | ||
// strict | ||
} | ||
// strict | ||
var LocaleResolver = function () { | ||
function LocaleResolver(detectors) { | ||
var transformers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
classCallCheck(this, LocaleResolver); | ||
this.locales = detectors.map(function (detector) { | ||
return detector.getLocales(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, []); | ||
var index$1 = ({ | ||
Transformer: Transformer, | ||
FallbacksTransformer: FallbacksTransformer, | ||
IETFTransformer: IETFTransformer, | ||
InvalidLocalesTransformer: InvalidLocalesTransformer, | ||
LanguageOnlyTransformer: LanguageOnlyTransformer, | ||
DefaultLocaleTransformer: DefaultLocaleTransformer, | ||
AllowOnlyTransformer: AllowOnlyTransformer | ||
}); | ||
this.transformers = transformers; | ||
} | ||
createClass(LocaleResolver, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
var transformers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var locales = this.locales; | ||
[].concat(toConsumableArray(this.transformers), toConsumableArray(transformers)).forEach(function (transformer) { | ||
locales = transformer.transform(locales); | ||
}); | ||
return locales.filter(function (value, index, array) { | ||
return array.indexOf(value) === index; | ||
}); | ||
} | ||
}]); | ||
return LocaleResolver; | ||
}(); | ||
// strict | ||
exports.FallbacksTransformer = FallbacksTransformer; | ||
exports.IETFTransformer = IETFTransformer; | ||
exports.InvalidLocalesTransformer = InvalidLocalesTransformer; | ||
exports.LanguageOnlyTransformer = LanguageOnlyTransformer; | ||
exports.DefaultLocaleTransformer = DefaultLocaleTransformer; | ||
exports.AllowOnlyTransformer = AllowOnlyTransformer; | ||
exports.NavigatorDetector = NavigatorDetector; | ||
exports.UrlDetector = UrlDetector; | ||
exports.FromListDetector = FromListDetector; | ||
exports.LocaleResolver = LocaleResolver; | ||
exports.DETECTORS = index; | ||
exports.TRANSFORMERS = index$1; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
return obj; | ||
} | ||
class LocaleResolver { | ||
constructor(detectors, transformers) { | ||
if (transformers === void 0) { | ||
transformers = []; | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
_defineProperty(this, "locales", void 0); | ||
var toConsumableArray = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
_defineProperty(this, "transformers", void 0); | ||
return arr2; | ||
} else { | ||
return Array.from(arr); | ||
this.locales = detectors.map(detector => { | ||
return detector.getLocales(); | ||
}).reduce((a, b) => a.concat(b), []); | ||
this.transformers = transformers; | ||
} | ||
}; | ||
// strict | ||
// strict | ||
// strict | ||
var LocaleResolver = function () { | ||
function LocaleResolver(detectors) { | ||
var transformers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
classCallCheck(this, LocaleResolver); | ||
this.locales = detectors.map(function (detector) { | ||
return detector.getLocales(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, []); | ||
this.transformers = transformers; | ||
getLocales(transformers) { | ||
if (transformers === void 0) { | ||
transformers = []; | ||
} | ||
createClass(LocaleResolver, [{ | ||
key: 'getLocales', | ||
value: function getLocales() { | ||
var transformers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var locales = this.locales; | ||
let locales = this.locales; | ||
[...this.transformers, ...transformers].forEach(transformer => { | ||
locales = transformer.transform(locales); | ||
}); | ||
return locales.filter((value, index, array) => { | ||
return array.indexOf(value) === index; | ||
}); | ||
} | ||
} | ||
[].concat(toConsumableArray(this.transformers), toConsumableArray(transformers)).forEach(function (transformer) { | ||
locales = transformer.transform(locales); | ||
}); | ||
return locales.filter(function (value, index, array) { | ||
return array.indexOf(value) === index; | ||
}); | ||
} | ||
}]); | ||
return LocaleResolver; | ||
}(); | ||
module.exports = LocaleResolver; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
// strict | ||
/** | ||
@@ -65,40 +27,31 @@ * This transformer allows you to filter locales | ||
var AllowOnlyTransformer = function (_Transformer) { | ||
inherits(AllowOnlyTransformer, _Transformer); | ||
class AllowOnlyTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {Array<string>} allowedLocales - list of allowed locales | ||
*/ | ||
constructor(allowedLocales) { | ||
super(); | ||
/** | ||
* Constructor. | ||
* | ||
* @param {Array<string>} allowedLocales - list of allowed locales | ||
*/ | ||
function AllowOnlyTransformer(allowedLocales) { | ||
classCallCheck(this, AllowOnlyTransformer); | ||
_defineProperty(this, "allowedLocales", void 0); | ||
var _this = possibleConstructorReturn(this, (AllowOnlyTransformer.__proto__ || Object.getPrototypeOf(AllowOnlyTransformer)).call(this)); | ||
this.allowedLocales = allowedLocales; | ||
} | ||
/** | ||
* Return only allowed locales. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} allowed locales | ||
*/ | ||
_this.allowedLocales = allowedLocales; | ||
return _this; | ||
} | ||
/** | ||
* Return only allowed locales. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} allowed locales | ||
*/ | ||
transform(locales) { | ||
return locales.filter(locale => { | ||
return this.allowedLocales.indexOf(locale) >= 0; | ||
}); | ||
} | ||
} | ||
createClass(AllowOnlyTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
return locales.filter(function (locale) { | ||
return _this2.allowedLocales.indexOf(locale) >= 0; | ||
}); | ||
} | ||
}]); | ||
return AllowOnlyTransformer; | ||
}(Transformer); | ||
module.exports = AllowOnlyTransformer; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
var toConsumableArray = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} else { | ||
return Array.from(arr); | ||
} | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
// strict | ||
/** | ||
@@ -75,49 +27,42 @@ * This transformer allows you to append default locale. | ||
var DefaultLocaleTransformer = function (_Transformer) { | ||
inherits(DefaultLocaleTransformer, _Transformer); | ||
class DefaultLocaleTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} defaultLocale - set default locale | ||
*/ | ||
constructor(defaultLocale) { | ||
super(); | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} defaultLocale - set default locale | ||
*/ | ||
function DefaultLocaleTransformer(defaultLocale) { | ||
classCallCheck(this, DefaultLocaleTransformer); | ||
_defineProperty(this, "defaultLocale", void 0); | ||
var _this = possibleConstructorReturn(this, (DefaultLocaleTransformer.__proto__ || Object.getPrototypeOf(DefaultLocaleTransformer)).call(this)); | ||
this.defaultLocale = defaultLocale; | ||
} | ||
/** | ||
* Add default locale to end of array | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with default | ||
*/ | ||
_this.defaultLocale = defaultLocale; | ||
return _this; | ||
} | ||
/** | ||
* Add default locale to end of array | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with default | ||
*/ | ||
transform(locales) { | ||
const transformedLocales = [...locales.map(locale => { | ||
const splitedLocale = locale.split('-'); | ||
return splitedLocale.map((value, index) => { | ||
const localeGenerator = []; | ||
createClass(DefaultLocaleTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var transformedLocales = [].concat(toConsumableArray(locales.map(function (locale) { | ||
var splitedLocale = locale.split('-'); | ||
return splitedLocale.map(function (value, index) { | ||
var localeGenerator = []; | ||
for (var i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, [])), [this.defaultLocale]); | ||
// slice locales that comes after default locale | ||
return transformedLocales.slice(0, transformedLocales.indexOf(this.defaultLocale) + 1); | ||
for (let i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
}]); | ||
return DefaultLocaleTransformer; | ||
}(Transformer); | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce((a, b) => a.concat(b), []), ...[this.defaultLocale]]; // slice locales that comes after default locale | ||
return transformedLocales.slice(0, transformedLocales.indexOf(this.defaultLocale) + 1); | ||
} | ||
} | ||
module.exports = DefaultLocaleTransformer; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
// strict | ||
/** | ||
@@ -67,37 +14,26 @@ * This transformers create fallbacks from complex locales to simpliest. | ||
var FallbacksTransformer = function (_Transformer) { | ||
inherits(FallbacksTransformer, _Transformer); | ||
class FallbacksTransformer extends Transformer { | ||
/** | ||
* Add fallbacks to locales. Locales should be in IETF language tag format. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with fallbacks | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
const splitedLocale = locale.split('-'); | ||
return splitedLocale.map((value, index) => { | ||
const localeGenerator = []; | ||
function FallbacksTransformer() { | ||
classCallCheck(this, FallbacksTransformer); | ||
return possibleConstructorReturn(this, (FallbacksTransformer.__proto__ || Object.getPrototypeOf(FallbacksTransformer)).apply(this, arguments)); | ||
} | ||
for (let i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
createClass(FallbacksTransformer, [{ | ||
key: 'transform', | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce((a, b) => a.concat(b), []); | ||
} | ||
/** | ||
* Add fallbacks to locales. Locales should be in IETF language tag format. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with fallbacks | ||
*/ | ||
value: function transform(locales) { | ||
return locales.map(function (locale) { | ||
var splitedLocale = locale.split('-'); | ||
return splitedLocale.map(function (value, index) { | ||
var localeGenerator = []; | ||
for (var i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, []); | ||
} | ||
}]); | ||
return FallbacksTransformer; | ||
}(Transformer); | ||
} | ||
module.exports = FallbacksTransformer; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
// strict | ||
/** | ||
@@ -65,43 +27,37 @@ * This transformer convert locales to standard IETF language tag. | ||
var IETFTransformer = function (_Transformer) { | ||
inherits(IETFTransformer, _Transformer); | ||
class IETFTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} localeSeparator - how is locales parts separated | ||
*/ | ||
constructor(localeSeparator) { | ||
if (localeSeparator === void 0) { | ||
localeSeparator = '-'; | ||
} | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} localeSeparator - how is locales parts separated | ||
*/ | ||
function IETFTransformer() { | ||
var localeSeparator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '-'; | ||
classCallCheck(this, IETFTransformer); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (IETFTransformer.__proto__ || Object.getPrototypeOf(IETFTransformer)).call(this)); | ||
_defineProperty(this, "localeSeparator", void 0); | ||
_this.localeSeparator = localeSeparator; | ||
return _this; | ||
} | ||
this.localeSeparator = localeSeparator; | ||
} | ||
/** | ||
* Convert locales to standard IETF language tag. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} list of transformed locales | ||
*/ | ||
/** | ||
* Convert locales to standard IETF language tag. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} list of transformed locales | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return locale.split(this.localeSeparator).map((value, index) => { | ||
return index === 1 ? value.toUpperCase() : value.toLowerCase(); | ||
}).join('-'); | ||
}); | ||
} | ||
createClass(IETFTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
} | ||
return locales.map(function (locale) { | ||
return locale.split(_this2.localeSeparator).map(function (value, index) { | ||
return index === 1 ? value.toUpperCase() : value.toLowerCase(); | ||
}).join('-'); | ||
}); | ||
} | ||
}]); | ||
return IETFTransformer; | ||
}(Transformer); | ||
module.exports = IETFTransformer; |
@@ -5,127 +5,71 @@ 'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
class Transformer {} | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
/** | ||
* This transformers create fallbacks from complex locales to simpliest. | ||
* 'en-US-east' => ['en-US-east', 'en-US', 'en'] | ||
* 'es-ES' => ['es-ES', 'es'] | ||
*/ | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
class FallbacksTransformer extends Transformer { | ||
/** | ||
* Add fallbacks to locales. Locales should be in IETF language tag format. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with fallbacks | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
const splitedLocale = locale.split('-'); | ||
return splitedLocale.map((value, index) => { | ||
const localeGenerator = []; | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (let i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce((a, b) => a.concat(b), []); | ||
} | ||
return target; | ||
}; | ||
} | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
var toConsumableArray = function (arr) { | ||
if (Array.isArray(arr)) { | ||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
return Array.from(arr); | ||
obj[key] = value; | ||
} | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
return obj; | ||
} | ||
// strict | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
/** | ||
* This transformers create fallbacks from complex locales to simpliest. | ||
* 'en-US-east' => ['en-US-east', 'en-US', 'en'] | ||
* 'es-ES' => ['es-ES', 'es'] | ||
*/ | ||
var FallbacksTransformer = function (_Transformer) { | ||
inherits(FallbacksTransformer, _Transformer); | ||
function FallbacksTransformer() { | ||
classCallCheck(this, FallbacksTransformer); | ||
return possibleConstructorReturn(this, (FallbacksTransformer.__proto__ || Object.getPrototypeOf(FallbacksTransformer)).apply(this, arguments)); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
createClass(FallbacksTransformer, [{ | ||
key: 'transform', | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
/** | ||
* Add fallbacks to locales. Locales should be in IETF language tag format. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with fallbacks | ||
*/ | ||
value: function transform(locales) { | ||
return locales.map(function (locale) { | ||
var splitedLocale = locale.split('-'); | ||
return splitedLocale.map(function (value, index) { | ||
var localeGenerator = []; | ||
for (var i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, []); | ||
} | ||
}]); | ||
return FallbacksTransformer; | ||
}(Transformer); | ||
return target; | ||
} | ||
// strict | ||
/** | ||
@@ -135,52 +79,43 @@ * This transformer convert locales to standard IETF language tag. | ||
var IETFTransformer = function (_Transformer) { | ||
inherits(IETFTransformer, _Transformer); | ||
class IETFTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} localeSeparator - how is locales parts separated | ||
*/ | ||
constructor(localeSeparator) { | ||
if (localeSeparator === void 0) { | ||
localeSeparator = '-'; | ||
} | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} localeSeparator - how is locales parts separated | ||
*/ | ||
function IETFTransformer() { | ||
var localeSeparator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '-'; | ||
classCallCheck(this, IETFTransformer); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (IETFTransformer.__proto__ || Object.getPrototypeOf(IETFTransformer)).call(this)); | ||
_defineProperty(this, "localeSeparator", void 0); | ||
_this.localeSeparator = localeSeparator; | ||
return _this; | ||
} | ||
this.localeSeparator = localeSeparator; | ||
} | ||
/** | ||
* Convert locales to standard IETF language tag. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} list of transformed locales | ||
*/ | ||
/** | ||
* Convert locales to standard IETF language tag. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} list of transformed locales | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return locale.split(this.localeSeparator).map((value, index) => { | ||
return index === 1 ? value.toUpperCase() : value.toLowerCase(); | ||
}).join('-'); | ||
}); | ||
} | ||
createClass(IETFTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
} | ||
return locales.map(function (locale) { | ||
return locale.split(_this2.localeSeparator).map(function (value, index) { | ||
return index === 1 ? value.toUpperCase() : value.toLowerCase(); | ||
}).join('-'); | ||
}); | ||
} | ||
}]); | ||
return IETFTransformer; | ||
}(Transformer); | ||
// strict | ||
var defaultConvertTable = { | ||
'es-XL': 'es-419', | ||
hindi: 'hi', | ||
indonesian: 'id', | ||
english: 'en' | ||
const defaultConvertTable = { | ||
'es-XL': 'es-419', | ||
hindi: 'hi', | ||
indonesian: 'id', | ||
english: 'en' | ||
}; | ||
/** | ||
@@ -190,43 +125,35 @@ * It convert invalid locales. | ||
var InvalidLocalesTransformer = function (_Transformer) { | ||
inherits(InvalidLocalesTransformer, _Transformer); | ||
class InvalidLocalesTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {SimpleObject} convertTable - simple convert table | ||
*/ | ||
constructor(convertTable) { | ||
if (convertTable === void 0) { | ||
convertTable = {}; | ||
} | ||
/** | ||
* Constructor. | ||
* | ||
* @param {SimpleObject} convertTable - simple convert table | ||
*/ | ||
function InvalidLocalesTransformer() { | ||
var convertTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, InvalidLocalesTransformer); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (InvalidLocalesTransformer.__proto__ || Object.getPrototypeOf(InvalidLocalesTransformer)).call(this)); | ||
_defineProperty(this, "convertTable", void 0); | ||
_this.convertTable = _extends({}, defaultConvertTable, convertTable); | ||
return _this; | ||
} | ||
this.convertTable = _objectSpread({}, defaultConvertTable, convertTable); | ||
} | ||
/** | ||
* Transform invalid languages. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} cleaned list of locales | ||
*/ | ||
/** | ||
* Transform invalid languages. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} cleaned list of locales | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return this.convertTable[locale] || locale; | ||
}); | ||
} | ||
createClass(InvalidLocalesTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
} | ||
return locales.map(function (locale) { | ||
return _this2.convertTable[locale] || locale; | ||
}); | ||
} | ||
}]); | ||
return InvalidLocalesTransformer; | ||
}(Transformer); | ||
// strict | ||
/** | ||
@@ -236,30 +163,17 @@ * Transform all locales to languages only with region variant. | ||
var LanguageOnlyTransformer = function (_Transformer) { | ||
inherits(LanguageOnlyTransformer, _Transformer); | ||
class LanguageOnlyTransformer extends Transformer { | ||
/** | ||
* Transform locales to languages only with region variant. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} - list of languages | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return locale.split('-')[0]; | ||
}); | ||
} | ||
function LanguageOnlyTransformer() { | ||
classCallCheck(this, LanguageOnlyTransformer); | ||
return possibleConstructorReturn(this, (LanguageOnlyTransformer.__proto__ || Object.getPrototypeOf(LanguageOnlyTransformer)).apply(this, arguments)); | ||
} | ||
} | ||
createClass(LanguageOnlyTransformer, [{ | ||
key: 'transform', | ||
/** | ||
* Transform locales to languages only with region variant. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} - list of languages | ||
*/ | ||
value: function transform(locales) { | ||
return locales.map(function (locale) { | ||
return locale.split('-')[0]; | ||
}); | ||
} | ||
}]); | ||
return LanguageOnlyTransformer; | ||
}(Transformer); | ||
// strict | ||
/** | ||
@@ -269,51 +183,42 @@ * This transformer allows you to append default locale. | ||
var DefaultLocaleTransformer = function (_Transformer) { | ||
inherits(DefaultLocaleTransformer, _Transformer); | ||
class DefaultLocaleTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} defaultLocale - set default locale | ||
*/ | ||
constructor(defaultLocale) { | ||
super(); | ||
/** | ||
* Constructor. | ||
* | ||
* @param {string} defaultLocale - set default locale | ||
*/ | ||
function DefaultLocaleTransformer(defaultLocale) { | ||
classCallCheck(this, DefaultLocaleTransformer); | ||
_defineProperty(this, "defaultLocale", void 0); | ||
var _this = possibleConstructorReturn(this, (DefaultLocaleTransformer.__proto__ || Object.getPrototypeOf(DefaultLocaleTransformer)).call(this)); | ||
this.defaultLocale = defaultLocale; | ||
} | ||
/** | ||
* Add default locale to end of array | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with default | ||
*/ | ||
_this.defaultLocale = defaultLocale; | ||
return _this; | ||
} | ||
/** | ||
* Add default locale to end of array | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} locales with default | ||
*/ | ||
transform(locales) { | ||
const transformedLocales = [...locales.map(locale => { | ||
const splitedLocale = locale.split('-'); | ||
return splitedLocale.map((value, index) => { | ||
const localeGenerator = []; | ||
createClass(DefaultLocaleTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var transformedLocales = [].concat(toConsumableArray(locales.map(function (locale) { | ||
var splitedLocale = locale.split('-'); | ||
return splitedLocale.map(function (value, index) { | ||
var localeGenerator = []; | ||
for (var i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}, [])), [this.defaultLocale]); | ||
// slice locales that comes after default locale | ||
return transformedLocales.slice(0, transformedLocales.indexOf(this.defaultLocale) + 1); | ||
for (let i = 0; i <= index; i++) { | ||
localeGenerator.push(splitedLocale[i]); | ||
} | ||
}]); | ||
return DefaultLocaleTransformer; | ||
}(Transformer); | ||
// strict | ||
return localeGenerator.join('-'); | ||
}).reverse(); | ||
}).reduce((a, b) => a.concat(b), []), ...[this.defaultLocale]]; // slice locales that comes after default locale | ||
return transformedLocales.slice(0, transformedLocales.indexOf(this.defaultLocale) + 1); | ||
} | ||
} | ||
/** | ||
@@ -323,42 +228,32 @@ * This transformer allows you to filter locales | ||
var AllowOnlyTransformer = function (_Transformer) { | ||
inherits(AllowOnlyTransformer, _Transformer); | ||
class AllowOnlyTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {Array<string>} allowedLocales - list of allowed locales | ||
*/ | ||
constructor(allowedLocales) { | ||
super(); | ||
/** | ||
* Constructor. | ||
* | ||
* @param {Array<string>} allowedLocales - list of allowed locales | ||
*/ | ||
function AllowOnlyTransformer(allowedLocales) { | ||
classCallCheck(this, AllowOnlyTransformer); | ||
_defineProperty(this, "allowedLocales", void 0); | ||
var _this = possibleConstructorReturn(this, (AllowOnlyTransformer.__proto__ || Object.getPrototypeOf(AllowOnlyTransformer)).call(this)); | ||
this.allowedLocales = allowedLocales; | ||
} | ||
/** | ||
* Return only allowed locales. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} allowed locales | ||
*/ | ||
_this.allowedLocales = allowedLocales; | ||
return _this; | ||
} | ||
/** | ||
* Return only allowed locales. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} allowed locales | ||
*/ | ||
transform(locales) { | ||
return locales.filter(locale => { | ||
return this.allowedLocales.indexOf(locale) >= 0; | ||
}); | ||
} | ||
} | ||
createClass(AllowOnlyTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
return locales.filter(function (locale) { | ||
return _this2.allowedLocales.indexOf(locale) >= 0; | ||
}); | ||
} | ||
}]); | ||
return AllowOnlyTransformer; | ||
}(Transformer); | ||
// strict | ||
exports.Transformer = Transformer; | ||
exports.FallbacksTransformer = FallbacksTransformer; | ||
@@ -365,0 +260,0 @@ exports.IETFTransformer = IETFTransformer; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return obj; | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var _extends = Object.assign || function (target) { | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
}; | ||
} | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
// strict | ||
var defaultConvertTable = { | ||
'es-XL': 'es-419', | ||
hindi: 'hi', | ||
indonesian: 'id', | ||
english: 'en' | ||
const defaultConvertTable = { | ||
'es-XL': 'es-419', | ||
hindi: 'hi', | ||
indonesian: 'id', | ||
english: 'en' | ||
}; | ||
/** | ||
@@ -86,41 +52,35 @@ * It convert invalid locales. | ||
var InvalidLocalesTransformer = function (_Transformer) { | ||
inherits(InvalidLocalesTransformer, _Transformer); | ||
class InvalidLocalesTransformer extends Transformer { | ||
/** | ||
* Constructor. | ||
* | ||
* @param {SimpleObject} convertTable - simple convert table | ||
*/ | ||
constructor(convertTable) { | ||
if (convertTable === void 0) { | ||
convertTable = {}; | ||
} | ||
/** | ||
* Constructor. | ||
* | ||
* @param {SimpleObject} convertTable - simple convert table | ||
*/ | ||
function InvalidLocalesTransformer() { | ||
var convertTable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
classCallCheck(this, InvalidLocalesTransformer); | ||
super(); | ||
var _this = possibleConstructorReturn(this, (InvalidLocalesTransformer.__proto__ || Object.getPrototypeOf(InvalidLocalesTransformer)).call(this)); | ||
_defineProperty(this, "convertTable", void 0); | ||
_this.convertTable = _extends({}, defaultConvertTable, convertTable); | ||
return _this; | ||
} | ||
this.convertTable = _objectSpread({}, defaultConvertTable, convertTable); | ||
} | ||
/** | ||
* Transform invalid languages. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} cleaned list of locales | ||
*/ | ||
/** | ||
* Transform invalid languages. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} cleaned list of locales | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return this.convertTable[locale] || locale; | ||
}); | ||
} | ||
createClass(InvalidLocalesTransformer, [{ | ||
key: 'transform', | ||
value: function transform(locales) { | ||
var _this2 = this; | ||
} | ||
return locales.map(function (locale) { | ||
return _this2.convertTable[locale] || locale; | ||
}); | ||
} | ||
}]); | ||
return InvalidLocalesTransformer; | ||
}(Transformer); | ||
module.exports = InvalidLocalesTransformer; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var inherits = function (subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
// strict | ||
/** | ||
@@ -65,28 +12,17 @@ * Transform all locales to languages only with region variant. | ||
var LanguageOnlyTransformer = function (_Transformer) { | ||
inherits(LanguageOnlyTransformer, _Transformer); | ||
class LanguageOnlyTransformer extends Transformer { | ||
/** | ||
* Transform locales to languages only with region variant. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} - list of languages | ||
*/ | ||
transform(locales) { | ||
return locales.map(locale => { | ||
return locale.split('-')[0]; | ||
}); | ||
} | ||
function LanguageOnlyTransformer() { | ||
classCallCheck(this, LanguageOnlyTransformer); | ||
return possibleConstructorReturn(this, (LanguageOnlyTransformer.__proto__ || Object.getPrototypeOf(LanguageOnlyTransformer)).apply(this, arguments)); | ||
} | ||
} | ||
createClass(LanguageOnlyTransformer, [{ | ||
key: 'transform', | ||
/** | ||
* Transform locales to languages only with region variant. | ||
* | ||
* @param {Array<string>} locales - list of locales | ||
* @returns {Array<string>} - list of languages | ||
*/ | ||
value: function transform(locales) { | ||
return locales.map(function (locale) { | ||
return locale.split('-')[0]; | ||
}); | ||
} | ||
}]); | ||
return LanguageOnlyTransformer; | ||
}(Transformer); | ||
module.exports = LanguageOnlyTransformer; |
'use strict'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
// strict | ||
/** | ||
* Transformer are used to change format of locale (eg. convert it to uppercase), create fallbacks or delete invalid items. | ||
*/ | ||
var Transformer = function Transformer() { | ||
classCallCheck(this, Transformer); | ||
}; | ||
class Transformer {} | ||
module.exports = Transformer; |
{ | ||
"name": "locales-detector", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Tool for detecting locales of visitors and transform them.", | ||
@@ -22,30 +22,23 @@ "keywords": [ | ||
"main": "dist/index.js", | ||
"jsnext:main": "dist/index.es.js", | ||
"module": "dist/index.esm.js", | ||
"types": "dist/index.d.ts", | ||
"devDependencies": { | ||
"@socifi/eslint-config": "^1.10.0", | ||
"@socifi/jest-config": "^1.10.0", | ||
"@socifi/rollup-config": "^1.10.0", | ||
"flow-bin": "^0.84.0", | ||
"flow-coverage-report": "^0.6.0", | ||
"flow-typed": "^2.5.1" | ||
"@socifi/babel-config": "^0.5.2", | ||
"@socifi/eslint-config": "^2.0.1", | ||
"@socifi/jest-config": "^2.0.0", | ||
"@socifi/rollup-config": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"@types/query-string": "^6.1.1", | ||
"query-string": "^6.2.0" | ||
}, | ||
"scripts": { | ||
"lint": "eslint ./src", | ||
"lint": "eslint ./src ./tests --ext .ts", | ||
"lint-export": "npm run lint -- -o ./tests/checkstyle/checkstyle.xml -f checkstyle", | ||
"test": "jest", | ||
"tsc": "tsc", | ||
"test-build": "npm run test -- --config jest.config.build.js", | ||
"flow": "flow", | ||
"flow-init": "flow-typed install query-string@6.x jest@22.x", | ||
"flow-coverage": "flow-coverage-report -f \"node_modules\\flow-bin\\vendor\\flow\" -i \"src/**/*.js\" -t html -t text -t json -o tests/coverage/flow", | ||
"build": "rollup --config ./rollup.config.js", | ||
"prepublishOnly": "npm run build && npm run test-build" | ||
}, | ||
"browserslist": [ | ||
"> 1%", | ||
"not ie <= 10", | ||
"not ie_mob <= 10" | ||
] | ||
"prepublishOnly": "npm run build && npm run tsc && npm run test-build" | ||
} | ||
} |
@@ -22,7 +22,5 @@ Locales Detector | ||
```js | ||
var localesDetector = require('locales-detector'); | ||
var LocaleResolver = localesDetector.LocaleResolver; | ||
var NavigatorDetector = localesDetector.NavigatorDetector; | ||
const { LocaleResolver, DETECTORS } = require('locales-detector'); | ||
console.log((new LocaleResolver([new NavigatorDetector()])).getLocales()); | ||
console.log((new LocaleResolver([new DETECTORS.NavigatorDetector()])).getLocales()); | ||
// output ['es', 'en] | ||
@@ -42,12 +40,9 @@ ``` | ||
```js | ||
var localesDetector = require('locales-detector'); | ||
var LocaleResolver = localesDetector.LocaleResolver; | ||
var NavigatorDetector = localesDetector.NavigatorDetector; | ||
var UrlDetector = localesDetector.UrlDetector; | ||
const { LocaleResolver, DETECTORS } = require('locales-detector'); | ||
// try to find lang settings in lang GET parameter | ||
var urlDetector = new UrlDetector('lang'); | ||
const urlDetector = new DETECTORS.UrlDetector('lang'); | ||
// try to find lang in browser settings | ||
var navigatorDetector = new NavigatorDetector(); | ||
const navigatorDetector = new DETECTORS.NavigatorDetector(); | ||
@@ -107,12 +102,6 @@ console.log((new LocaleResolver([urlDetector, navigatorDetector])).getLocales()); | ||
```js | ||
var localesDetector = require('locales-detector'); | ||
var LocaleResolver = localesDetector.LocaleResolver; | ||
var NavigatorDetector = localesDetector.NavigatorDetector; | ||
var UrlDetector = localesDetector.UrlDetector; | ||
var InvalidLocalesTransformer = localesDetector.InvalidLocalesTransformer; | ||
var FallbacksTransformer = localesDetector.FallbacksTransformer; | ||
const { LocaleResolver, DETECTORS, TRANSFORMERS } = require('locales-detector'); | ||
console.log((new LocaleResolver([new UrlDetector('lang'), new NavigatorDetector()], [new InvalidLocalesTransformer(), new FallbacksTransformer()])).getLocales()); | ||
console.log((new LocaleResolver([new DETECTORS.UrlDetector('lang'), new DETECTORS.NavigatorDetector()], [new TRANSFORMERS.InvalidLocalesTransformer(), new TRANSFORMERS.FallbacksTransformer()])).getLocales()); | ||
// output ['es-419', 'es', 'en'] | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
4
64
2805
97805
2
105
1
+ Added@types/query-string@^6.1.1
+ Added@types/query-string@6.3.0(transitive)