make-plural
Advanced tools
Comparing version 3.0.0-rc3 to 3.0.0-rc4
@@ -166,6 +166,8 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MakePlural = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
if (!this.ordinals && !this.cardinals) throw new Error("At least one type of plural is required"); | ||
this.categories = { cardinal: [], ordinal: [] }; | ||
this.parser = new Parser(); | ||
this.tests = new Tests(this); | ||
this.fn = this.buildFunction(); | ||
this.fn.obj = this; | ||
this.fn._obj = this; | ||
this.fn.categories = this.categories; | ||
this.fn.test = (function () { | ||
@@ -185,2 +187,3 @@ return this.tests.testAll() && this.fn; | ||
if (req) throw new Error("Locale \"" + this.lc + "\" " + type + " rules not found"); | ||
this.categories[type] = ["other"]; | ||
return "'other'"; | ||
@@ -199,2 +202,5 @@ } | ||
} | ||
this.categories[type] = cases.map(function (c) { | ||
return c[1]; | ||
}).concat("other"); | ||
if (cases.length === 1) { | ||
@@ -201,0 +207,0 @@ return "(" + cases[0][0] + ") ? '" + cases[0][1] + "' : 'other'"; |
@@ -1,1 +0,1 @@ | ||
!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var r;r="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,r.MakePlural=n()}}(function(){return function n(r,t,i){function e(o,s){if(!t[o]){if(!r[o]){var u="function"==typeof require&&require;if(!s&&u)return u(o,!0);if(a)return a(o,!0);var l=new Error("Cannot find module '"+o+"'");throw l.code="MODULE_NOT_FOUND",l}var c=t[o]={exports:{}};r[o][0].call(c.exports,function(n){var t=r[o][1][n];return e(t?t:n)},c,c.exports,n,r,t,i)}return t[o].exports}for(var a="function"==typeof require&&require,o=0;o<i.length;o++)e(i[o]);return e}({1:[function(n,r,t){"use strict";var i=function(n){return Array.isArray(n)?n:Array.from(n)},e=function(n){if(Array.isArray(n)){for(var r=0,t=Array(n.length);r<n.length;r++)t[r]=n[r];return t}return Array.from(n)},a=function(){function n(n,r){for(var t in r){var i=r[t];i.configurable=!0,i.value&&(i.writable=!0)}Object.defineProperties(n,r)}return function(r,t,i){return t&&n(r.prototype,t),i&&n(r,i),r}}(),o=function(n,r){if(!(n instanceof r))throw new TypeError("Cannot call a class as a function")},s=function(){function n(){o(this,n)}return a(n,{parse:{value:function(n){var r=this;return"i = 0 or n = 1"===n?"n >= 0 && n <= 1":"i = 0,1"===n?"n >= 0 && n < 2":"i = 1 and v = 0"===n?(this.v0=1,"n == 1 && v0"):n.replace(/([tv]) (!?)= 0/g,function(n,t,i){var e=t+"0";return r[e]=1,i?"!"+e:e}).replace(/\b[fintv]\b/g,function(n){return r[n]=1,n}).replace(/([fin]) % (10+)/g,function(n,t,i){var e=t+i;return r[e]=1,e}).replace(/n10+ = 0/g,"t0 && $&").replace(/(\w+ (!?)= )([0-9.]+,[0-9.,]+)/g,function(n,r,t,i){return"n = 0,1"===n?"(n == 0 || n == 1)":t?r+i.split(",").join(" && "+r):"("+r+i.split(",").join(" || "+r)+")"}).replace(/(\w+) (!?)= ([0-9]+)\.\.([0-9]+)/g,function(n,t,i,e,a){return Number(e)+1===Number(a)?i?""+t+" != "+e+" && "+t+" != "+a:"("+t+" == "+e+" || "+t+" == "+a+")":i?"("+t+" < "+e+" || "+t+" > "+a+")":"n"===t?(r.t0=1,"(t0 && n >= "+e+" && n <= "+a+")"):"("+t+" >= "+e+" && "+t+" <= "+a+")"}).replace(/ and /g," && ").replace(/ or /g," || ").replace(/ = /g," == ")}},vars:{value:function r(){var r=[];this.i&&r.push("i = s[0]"),(this.f||this.v)&&r.push("f = s[1] || ''"),this.t&&r.push("t = (s[1] || '').replace(/0+$/, '')"),this.v&&r.push("v = f.length"),this.v0&&r.push("v0 = !s[1]"),(this.t0||this.n10||this.n100)&&r.push("t0 = Number(s[0]) == n");for(var n in this)if(/^.10+$/.test(n)){var t="n"===n[0]?"t0 && s[0]":n[0];r.push(""+n+" = "+t+".slice(-"+n.substr(2).length+")")}return r.length?"var "+["s = String(n).split('.')"].concat(r).join(", "):""}}}),n}(),u=function(){function n(r){o(this,n),this.obj=r,this.ordinal={},this.cardinal={}}return a(n,{add:{value:function(n,r,t){this[n][r]=t.join(" ").replace(/^[ ,]+|[ ,…]+$/g,"").replace(/(0\.[0-9])~(1\.[1-9])/g,"$1 1.0 $2").split(/[ ,~…]+/)}},testCond:{value:function(n,r,t,i){try{var e=(i||this.obj.fn)(n,"ordinal"===r)}catch(a){e=a.toString()}if(e!==t)throw new Error("Locale "+JSON.stringify(this.obj.lc)+r+" rule self-test failed for v = "+JSON.stringify(n)+" (was "+JSON.stringify(e)+", expected "+JSON.stringify(t)+")");return!0}},testCat:{value:function(n,r,t){var i=this;return this[n][r].forEach(function(e){i.testCond(e,n,r,t),/\.0+$/.test(e)||i.testCond(Number(e),n,r,t)}),!0}},testAll:{value:function(){for(var n in this.cardinal)this.testCat("cardinal",n);for(var n in this.ordinal)this.testCat("ordinal",n);return!0}}}),n}(),l=function(){function n(r){var t=void 0===arguments[1]?{}:arguments[1];if(o(this,n),"object"==typeof r&&(t=r,r=t.lc),this.lc=r||n.lc,this.cardinals=t.cardinals||n.cardinals,this.ordinals=t.ordinals||n.ordinals,!this.ordinals&&!this.cardinals)throw new Error("At least one type of plural is required");return this.parser=new s,this.tests=new u(this),this.fn=this.buildFunction(),this.fn.obj=this,this.fn.test=function(){return this.tests.testAll()&&this.fn}.bind(this),this.fn.toString=this.fnToString.bind(this),this.fn}return a(n,{compile:{value:function(r,t){var a=[],o=n.rules[r][this.lc];if(!o){if(t)throw new Error('Locale "'+this.lc+'" '+r+" rules not found");return"'other'"}for(var s in o){var u=o[s].trim().split(/\s*@\w*/),l=i(u),c=l[0],f=l.slice(1),h=s.replace("pluralRule-count-","");c&&a.push([this.parser.parse(c),h]),this.tests.add(r,h,f)}return 1===a.length?"("+a[0][0]+") ? '"+a[0][1]+"' : 'other'":[].concat(e(a.map(function(n){return"("+n[0]+") ? '"+n[1]+"'"})),["'other'"]).join("\n : ")}},buildFunction:{value:function(){var n=this,r=function(r){return r?(r[1]?"return ":"if (ord) return ")+n.compile.apply(n,e(r)):""},t={vars:function(n){return(" "+n+";").replace(/(.{1,78})(,|$) ?/g,"$1$2\n ")},cond:function(n){return(" "+n+";").replace(/(.{1,78}) (\|\| |$) ?/gm,"$1\n $2")}},i=[this.ordinals&&["ordinal",!this.cardinals],this.cardinals&&["cardinal",!0]].map(r).map(t.cond),a=[t.vars(this.parser.vars())].concat(e(i)).join("\n").replace(/\s+$/gm,"").replace(/^[\s;]*[\r\n]+/gm,""),o=this.ordinals&&this.cardinals?"n, ord":"n";return new Function(o,a)}},fnToString:{value:function(n){return Function.prototype.toString.call(this.fn).replace(/^function( \w+)?/,n?"function "+n:"function").replace("\n/**/","")}}},{load:{value:function(){for(var r=arguments.length,t=Array(r),i=0;r>i;i++)t[i]=arguments[i];return t.forEach(function(r){var t=r&&r.supplemental||null;if(!t)throw new Error("Data does not appear to be CLDR data");n.rules={cardinal:t["plurals-type-cardinal"]||n.rules.cardinal,ordinal:t["plurals-type-ordinal"]||n.rules.ordinal}}),n}}}),n}();r.exports=l,l.cardinals=!0,l.ordinals=!1,l.rules={cardinal:{},ordinal:{}}},{}]},{},[1])(1)}); | ||
!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var r;r="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,r.MakePlural=n()}}(function(){return function n(r,t,i){function e(o,s){if(!t[o]){if(!r[o]){var u="function"==typeof require&&require;if(!s&&u)return u(o,!0);if(a)return a(o,!0);var l=new Error("Cannot find module '"+o+"'");throw l.code="MODULE_NOT_FOUND",l}var c=t[o]={exports:{}};r[o][0].call(c.exports,function(n){var t=r[o][1][n];return e(t?t:n)},c,c.exports,n,r,t,i)}return t[o].exports}for(var a="function"==typeof require&&require,o=0;o<i.length;o++)e(i[o]);return e}({1:[function(n,r,t){"use strict";var i=function(n){return Array.isArray(n)?n:Array.from(n)},e=function(n){if(Array.isArray(n)){for(var r=0,t=Array(n.length);r<n.length;r++)t[r]=n[r];return t}return Array.from(n)},a=function(){function n(n,r){for(var t in r){var i=r[t];i.configurable=!0,i.value&&(i.writable=!0)}Object.defineProperties(n,r)}return function(r,t,i){return t&&n(r.prototype,t),i&&n(r,i),r}}(),o=function(n,r){if(!(n instanceof r))throw new TypeError("Cannot call a class as a function")},s=function(){function n(){o(this,n)}return a(n,{parse:{value:function(n){var r=this;return"i = 0 or n = 1"===n?"n >= 0 && n <= 1":"i = 0,1"===n?"n >= 0 && n < 2":"i = 1 and v = 0"===n?(this.v0=1,"n == 1 && v0"):n.replace(/([tv]) (!?)= 0/g,function(n,t,i){var e=t+"0";return r[e]=1,i?"!"+e:e}).replace(/\b[fintv]\b/g,function(n){return r[n]=1,n}).replace(/([fin]) % (10+)/g,function(n,t,i){var e=t+i;return r[e]=1,e}).replace(/n10+ = 0/g,"t0 && $&").replace(/(\w+ (!?)= )([0-9.]+,[0-9.,]+)/g,function(n,r,t,i){return"n = 0,1"===n?"(n == 0 || n == 1)":t?r+i.split(",").join(" && "+r):"("+r+i.split(",").join(" || "+r)+")"}).replace(/(\w+) (!?)= ([0-9]+)\.\.([0-9]+)/g,function(n,t,i,e,a){return Number(e)+1===Number(a)?i?""+t+" != "+e+" && "+t+" != "+a:"("+t+" == "+e+" || "+t+" == "+a+")":i?"("+t+" < "+e+" || "+t+" > "+a+")":"n"===t?(r.t0=1,"(t0 && n >= "+e+" && n <= "+a+")"):"("+t+" >= "+e+" && "+t+" <= "+a+")"}).replace(/ and /g," && ").replace(/ or /g," || ").replace(/ = /g," == ")}},vars:{value:function r(){var r=[];this.i&&r.push("i = s[0]"),(this.f||this.v)&&r.push("f = s[1] || ''"),this.t&&r.push("t = (s[1] || '').replace(/0+$/, '')"),this.v&&r.push("v = f.length"),this.v0&&r.push("v0 = !s[1]"),(this.t0||this.n10||this.n100)&&r.push("t0 = Number(s[0]) == n");for(var n in this)if(/^.10+$/.test(n)){var t="n"===n[0]?"t0 && s[0]":n[0];r.push(""+n+" = "+t+".slice(-"+n.substr(2).length+")")}return r.length?"var "+["s = String(n).split('.')"].concat(r).join(", "):""}}}),n}(),u=function(){function n(r){o(this,n),this.obj=r,this.ordinal={},this.cardinal={}}return a(n,{add:{value:function(n,r,t){this[n][r]=t.join(" ").replace(/^[ ,]+|[ ,…]+$/g,"").replace(/(0\.[0-9])~(1\.[1-9])/g,"$1 1.0 $2").split(/[ ,~…]+/)}},testCond:{value:function(n,r,t,i){try{var e=(i||this.obj.fn)(n,"ordinal"===r)}catch(a){e=a.toString()}if(e!==t)throw new Error("Locale "+JSON.stringify(this.obj.lc)+r+" rule self-test failed for v = "+JSON.stringify(n)+" (was "+JSON.stringify(e)+", expected "+JSON.stringify(t)+")");return!0}},testCat:{value:function(n,r,t){var i=this;return this[n][r].forEach(function(e){i.testCond(e,n,r,t),/\.0+$/.test(e)||i.testCond(Number(e),n,r,t)}),!0}},testAll:{value:function(){for(var n in this.cardinal)this.testCat("cardinal",n);for(var n in this.ordinal)this.testCat("ordinal",n);return!0}}}),n}(),l=function(){function n(r){var t=void 0===arguments[1]?{}:arguments[1];if(o(this,n),"object"==typeof r&&(t=r,r=t.lc),this.lc=r||n.lc,this.cardinals=t.cardinals||n.cardinals,this.ordinals=t.ordinals||n.ordinals,!this.ordinals&&!this.cardinals)throw new Error("At least one type of plural is required");return this.categories={cardinal:[],ordinal:[]},this.parser=new s,this.tests=new u(this),this.fn=this.buildFunction(),this.fn._obj=this,this.fn.categories=this.categories,this.fn.test=function(){return this.tests.testAll()&&this.fn}.bind(this),this.fn.toString=this.fnToString.bind(this),this.fn}return a(n,{compile:{value:function(r,t){var a=[],o=n.rules[r][this.lc];if(!o){if(t)throw new Error('Locale "'+this.lc+'" '+r+" rules not found");return this.categories[r]=["other"],"'other'"}for(var s in o){var u=o[s].trim().split(/\s*@\w*/),l=i(u),c=l[0],f=l.slice(1),h=s.replace("pluralRule-count-","");c&&a.push([this.parser.parse(c),h]),this.tests.add(r,h,f)}return this.categories[r]=a.map(function(n){return n[1]}).concat("other"),1===a.length?"("+a[0][0]+") ? '"+a[0][1]+"' : 'other'":[].concat(e(a.map(function(n){return"("+n[0]+") ? '"+n[1]+"'"})),["'other'"]).join("\n : ")}},buildFunction:{value:function(){var n=this,r=function(r){return r?(r[1]?"return ":"if (ord) return ")+n.compile.apply(n,e(r)):""},t={vars:function(n){return(" "+n+";").replace(/(.{1,78})(,|$) ?/g,"$1$2\n ")},cond:function(n){return(" "+n+";").replace(/(.{1,78}) (\|\| |$) ?/gm,"$1\n $2")}},i=[this.ordinals&&["ordinal",!this.cardinals],this.cardinals&&["cardinal",!0]].map(r).map(t.cond),a=[t.vars(this.parser.vars())].concat(e(i)).join("\n").replace(/\s+$/gm,"").replace(/^[\s;]*[\r\n]+/gm,""),o=this.ordinals&&this.cardinals?"n, ord":"n";return new Function(o,a)}},fnToString:{value:function(n){return Function.prototype.toString.call(this.fn).replace(/^function( \w+)?/,n?"function "+n:"function").replace("\n/**/","")}}},{load:{value:function(){for(var r=arguments.length,t=Array(r),i=0;r>i;i++)t[i]=arguments[i];return t.forEach(function(r){var t=r&&r.supplemental||null;if(!t)throw new Error("Data does not appear to be CLDR data");n.rules={cardinal:t["plurals-type-cardinal"]||n.rules.cardinal,ordinal:t["plurals-type-ordinal"]||n.rules.ordinal}}),n}}}),n}();r.exports=l,l.cardinals=!0,l.ordinals=!1,l.rules={cardinal:{},ordinal:{}}},{}]},{},[1])(1)}); |
{ | ||
"name": "make-plural", | ||
"version": "3.0.0-rc3", | ||
"version": "3.0.0-rc4", | ||
"description": "Translates Unicode CLDR pluralization rules to executable JavaScript", | ||
@@ -65,2 +65,3 @@ "keywords": [ | ||
"no-new-func": 0, | ||
"no-underscore-dangle": 0, | ||
"quotes": [ | ||
@@ -73,3 +74,6 @@ 1, | ||
} | ||
}, | ||
"optionalDependencies": { | ||
"minimist": "^1.1.1" | ||
} | ||
} |
@@ -31,3 +31,2 @@ var _cp = [ | ||
}(this, { | ||
af: _cp[1], | ||
@@ -950,3 +949,2 @@ | ||
} | ||
})); |
@@ -43,7 +43,7 @@ [![ISC License](https://img.shields.io/npm/l/make-plural.svg)](http://en.wikipedia.org/wiki/ISC_license) | ||
## `plurals.js` - Precompiled plurals | ||
## Precompiled plurals | ||
Contains an UMD module that can be included with node's `require` or AMD's | ||
`define`. In a browser environment, will populate a global object `plurals`. | ||
Said module contains 199 functions (one per | ||
`plurals.js` contains an UMD module that can be included with node's `require` | ||
or AMD's `define`. In a browser environment, it will populate a global object | ||
`plurals`. Said module contains 199 functions (one per | ||
[language](http://www.unicode.org/cldr/charts/27/supplemental/language_plural_rules.html)), | ||
@@ -54,6 +54,10 @@ each taking as a first parameter the value to be classified (either a number or | ||
If your language isn't directly included, try removing any trailing parts that | ||
are separated from the stem by `-` or `_`. | ||
`pluralCategories.js` has a similar structure to `plurals.js`, but contains an | ||
array of the pluralization categories the cardinal and ordinal rules each | ||
language's pluralization function may output. | ||
If your language isn't directly included in either of the above, try removing | ||
any trailing parts that are separated from the stem by `-` or `_`. | ||
### Precompiled use: Node | ||
@@ -66,3 +70,3 @@ | ||
am: [Function], | ||
// snip 193 lines... | ||
// snip 193 lines... | ||
yo: [Function], | ||
@@ -91,2 +95,13 @@ zh: [Function], | ||
} | ||
> pluralCategories = require('make-plural/pluralCategories') | ||
{ af: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] }, | ||
ak: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] }, | ||
am: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] }, | ||
ar: | ||
{ cardinal: [ 'zero', 'one', 'two', 'few', 'many', 'other' ], | ||
ordinal: [ 'other' ] }, | ||
// snip 255 lines... | ||
zh: { cardinal: [ 'other' ], ordinal: [ 'other' ] }, | ||
zu: { cardinal: [ 'one', 'other' ], ordinal: [ 'other' ] } } | ||
``` | ||
@@ -130,4 +145,16 @@ | ||
## `make-plural.js` - Live compiler | ||
## Live compiler: `make-plural.js` | ||
### MakePlural.load(cldr, ...) | ||
Loads CLDR rules from one or more `cldr` variables, each of which must be an | ||
object formatted like | ||
[this](https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/plurals.json). | ||
No plural data is included by default, so you'll need to call this at least | ||
once, or otherwise fill the `MakePlural.rules` object. | ||
The default CLDR rules are included in make-plural, and may be loaded as seen | ||
in the examples below. | ||
### new MakePlural(lc, opt) | ||
@@ -155,17 +182,3 @@ Returns a function that takes an argument `n` and returns its plural category | ||
### MakePlural.load(cldr, ...) | ||
Loads CLDR rules from one or more `cldr` variables, each of which may be an | ||
object or the path to a JSON file formatted like | ||
[this](https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/plurals.json). | ||
The stored rules are kept in `MakePlural.rules.cardinal` and | ||
`MakePlural.rules.ordinal`, which may also be directly accessed. | ||
If called within a context where `request()` isn't available and `cldr` is a | ||
string, it's taken as the URL of the JSON file that'll be fetched and parsed | ||
using a synchronous `XMLHttpRequest`. | ||
By default, `MakePlural()` will call `MakePlural.load(cldr)` when required, | ||
using the rules included in `data/`, `plurals.json` and `ordinals.json`. | ||
### Live use: Node | ||
@@ -175,3 +188,4 @@ | ||
> MakePlural = require('make-plural/make-plural').load( | ||
... require('./data/plurals.json'), require('./data/ordinals.json')) | ||
... require('make-plural/data/plurals.json'), | ||
... require('make-plural/data/ordinals.json')) | ||
{ [Function: MakePlural] | ||
@@ -200,9 +214,11 @@ cardinals: true, | ||
{ [Function] | ||
obj: | ||
_obj: | ||
{ lc: 'sk', | ||
cardinals: true, | ||
ordinals: false, | ||
categories: { cardinal: [Object], ordinal: [] }, | ||
parser: { v0: 1, i: 1 }, | ||
tests: { obj: [Circular], ordinal: {}, cardinal: [Object] }, | ||
fn: [Circular] }, | ||
categories: { cardinal: [ 'one', 'few', 'many', 'other' ], ordinal: [] }, | ||
test: [Function], | ||
@@ -240,2 +256,4 @@ toString: [Function] } | ||
```sh | ||
$ ./bin/make-plural > plurals.js | ||
$ ./bin/make-plural fr | ||
@@ -247,6 +265,6 @@ function fr(n, ord) { | ||
$ ./bin/make-plural fr 1.5 | ||
$ ./bin/make-plural --locale fr --value 1.5 | ||
one | ||
$ ./bin/make-plural fr 1.5 true | ||
$ ./bin/make-plural 1.5 -l fr --ordinal | ||
other | ||
@@ -261,7 +279,11 @@ ``` | ||
Make-plural has no runtime dependencies. CLDR plural rule data is included in | ||
JSON format; make-plural supports the | ||
Make-plural has no required runtime dependencies. CLDR plural rule data is | ||
included in JSON format; make-plural supports the | ||
[LDML Language Plural Rules](http://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) | ||
as used in CLDR release 24 and later. | ||
The CLI binary `bin/make-plural` does use | ||
[minimist](https://www.npmjs.com/package/minimist) as an argument parser, but | ||
that is not required for any other use. | ||
Using `MakePlural.load()`, you may make use of external sources of CLDR data. | ||
@@ -268,0 +290,0 @@ For example, the following works when using together with |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147500
2156
291
1