make-plural
Advanced tools
Comparing version 3.0.0-rc2 to 3.0.0-rc3
@@ -0,0 +0,0 @@ { |
@@ -0,1 +1,2 @@ | ||
(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){ | ||
"use strict"; | ||
@@ -98,42 +99,2 @@ | ||
var Rules = (function () { | ||
function Rules() { | ||
_classCallCheck(this, Rules); | ||
this.data = {}; | ||
this.rootPath = "./data/"; | ||
} | ||
_createClass(Rules, { | ||
loadData: { | ||
value: function loadData(cldr) { | ||
var data = cldr && cldr.supplemental || {}; | ||
this.data = { | ||
cardinal: data["plurals-type-cardinal"] || this.data.cardinal, | ||
ordinal: data["plurals-type-ordinal"] || this.data.ordinal | ||
}; | ||
return this.data; | ||
} | ||
}, | ||
loadPath: { | ||
value: function loadPath(path) { | ||
if (path.indexOf("/") === -1) path = this.rootPath + path; | ||
return this.loadData(require(path)); | ||
} | ||
}, | ||
cardinal: { | ||
get: function () { | ||
return this.data.cardinal || this.loadPath("plurals.json").cardinal; | ||
} | ||
}, | ||
ordinal: { | ||
get: function () { | ||
return this.data.ordinal || this.loadPath("ordinals.json").ordinal; | ||
} | ||
} | ||
}); | ||
return Rules; | ||
})(); | ||
var Tests = (function () { | ||
@@ -155,5 +116,5 @@ function Tests(obj) { | ||
testCond: { | ||
value: function testCond(n, type, expResult) { | ||
value: function testCond(n, type, expResult, fn) { | ||
try { | ||
var r = this.obj.fn(n, type === "ordinal"); | ||
var r = (fn || this.obj.fn)(n, type === "ordinal"); | ||
} catch (e) { | ||
@@ -169,8 +130,8 @@ r = e.toString(); | ||
testCat: { | ||
value: function testCat(type, cat) { | ||
value: function testCat(type, cat, fn) { | ||
var _this = this; | ||
this[type][cat].forEach(function (n) { | ||
_this.testCond(n, type, cat); | ||
if (!/\.0+$/.test(n)) _this.testCond(Number(n), type, cat); | ||
_this.testCond(n, type, cat, fn); | ||
if (!/\.0+$/.test(n)) _this.testCond(Number(n), type, cat, fn); | ||
}); | ||
@@ -253,5 +214,3 @@ return true; | ||
var compile = function (c) { | ||
var _ref; | ||
return c ? (c[1] ? "return " : "if (ord) return ") + (_ref = _this).compile.apply(_ref, _toConsumableArray(c)) : ""; | ||
return c ? (c[1] ? "return " : "if (ord) return ") + _this.compile.apply(_this, _toConsumableArray(c)) : ""; | ||
}, | ||
@@ -282,4 +241,9 @@ fold = { vars: function (str) { | ||
args.forEach(function (arg) { | ||
if (typeof arg == "string") MakePlural.rules.loadPath(arg);else MakePlural.rules.loadData(arg); | ||
args.forEach(function (cldr) { | ||
var data = cldr && cldr.supplemental || null; | ||
if (!data) throw new Error("Data does not appear to be CLDR data"); | ||
MakePlural.rules = { | ||
cardinal: data["plurals-type-cardinal"] || MakePlural.rules.cardinal, | ||
ordinal: data["plurals-type-ordinal"] || MakePlural.rules.ordinal | ||
}; | ||
}); | ||
@@ -298,2 +262,5 @@ return MakePlural; | ||
MakePlural.ordinals = false; | ||
MakePlural.rules = new Rules(); | ||
MakePlural.rules = { cardinal: {}, ordinal: {} }; | ||
},{}]},{},[1])(1) | ||
}); |
{ | ||
"name": "make-plural", | ||
"version": "3.0.0-rc3", | ||
"description": "Translates Unicode CLDR pluralization rules to executable JavaScript", | ||
@@ -24,3 +25,4 @@ "keywords": [ | ||
"data/", | ||
"make-plural.js" | ||
"make-plural.*", | ||
"plurals.*" | ||
], | ||
@@ -30,3 +32,3 @@ "bin": { | ||
}, | ||
"main": "make-plural", | ||
"main": "plurals", | ||
"scripts": { | ||
@@ -39,4 +41,5 @@ "lint": "eslint src/", | ||
"babel-eslint": "*", | ||
"babelify": "^5.0.4", | ||
"browserify": "^9.0.3", | ||
"cldr-core": "unicode-cldr/cldr-core#27.0.0", | ||
"cldr-core": "^27.0.1", | ||
"eslint": "*", | ||
@@ -72,4 +75,3 @@ "expect.js": "^0.3.1", | ||
} | ||
}, | ||
"version": "3.0.0-rc2" | ||
} | ||
} |
246
README.md
@@ -0,12 +1,21 @@ | ||
[![ISC License](https://img.shields.io/npm/l/make-plural.svg)](http://en.wikipedia.org/wiki/ISC_license) | ||
[![Build Status](https://travis-ci.org/eemeli/make-plural.js.svg?branch=master)](https://travis-ci.org/eemeli/make-plural.js) | ||
make-plural | ||
=========== | ||
[![ISC License](https://img.shields.io/npm/l/make-plural.svg)](http://en.wikipedia.org/wiki/ISC_license) | ||
[![Build Status](https://travis-ci.org/eemeli/make-plural.js.svg?branch=master)](https://travis-ci.org/eemeli/make-plural.js) | ||
Make-plural is a JavaScript module that translates [Unicode CLDR](http://cldr.unicode.org/) | ||
Make-plural is a JavaScript module that translates | ||
[Unicode CLDR](http://cldr.unicode.org/) | ||
[pluralization rules](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html) | ||
to JavaScript functions. | ||
to JavaScript functions. It includes both a live parser (`make-plural.js`) as | ||
well as the generated output for the latest edition of the CLDR (`plurals.js`); | ||
the latter is just over 2kB in size when minified & gzipped, and covers 199 | ||
languages, so it's probably what you want unless you really know what you're | ||
doing. | ||
It's written in [ECMAScript 6](https://people.mozilla.org/~jorendorff/es6-draft.html) and transpiled using [Babel](https://babeljs.io/) to CommonJS, AMD and ES6 module formats, as well as being suitable for use in browser environments. | ||
Make-plural is written in | ||
[ECMAScript 6](https://people.mozilla.org/~jorendorff/es6-draft.html) and | ||
transpiled using [Babel](https://babeljs.io/) and | ||
[Browserify](http://browserify.org/) to CommonJS and AMD and ES6 module formats, | ||
as well as being suitable for use in browser environments. | ||
@@ -19,7 +28,7 @@ | ||
``` | ||
or | ||
_or_ | ||
``` | ||
bower install make-plural | ||
``` | ||
or | ||
_or_ | ||
``` | ||
@@ -31,66 +40,43 @@ git clone https://github.com/eemeli/make-plural.js.git | ||
``` | ||
_or_ download the latest release from | ||
[here](https://github.com/eemeli/make-plural.js/releases/latest) | ||
## Usage: Node | ||
## `plurals.js` - Precompiled plurals | ||
```js | ||
> MakePlural = require('make-plural') | ||
{ [Function: MakePlural] | ||
cardinals: true, | ||
ordinals: false, | ||
rules: { data: {}, rootPath: './data/' } } | ||
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 | ||
[language](http://www.unicode.org/cldr/charts/27/supplemental/language_plural_rules.html)), | ||
each taking as a first parameter the value to be classified (either a number or | ||
a string), and as an optional second parameter, a boolean that if true, applies | ||
ordinal rather than cardinal rules. | ||
> sk = new MakePlural('sk') | ||
{ [Function] | ||
obj: | ||
{ lc: 'sk', | ||
cardinals: true, | ||
ordinals: false, | ||
parser: { v0: 1, i: 1 }, | ||
tests: { obj: [Circular], ordinal: {}, cardinal: [Object] }, | ||
fn: [Circular] }, | ||
test: [Function], | ||
toString: [Function] } | ||
If your language isn't directly included, try removing any trailing parts that | ||
are separated from the stem by `-` or `_`. | ||
> sk(1) | ||
'one' | ||
> sk(3.0) | ||
'few' | ||
### Precompiled use: Node | ||
> sk('1.0') | ||
'many' | ||
```js | ||
> plurals = require('make-plural/plurals') | ||
{ af: [Function], | ||
ak: [Function], | ||
am: [Function], | ||
// snip 193 lines... | ||
yo: [Function], | ||
zh: [Function], | ||
zu: [Function] } | ||
> sk('0') | ||
'other' | ||
> plurals.en(1) // 1st param is the value | ||
'one' | ||
> console.log(sk.toString()) | ||
function(n) { | ||
var s = String(n).split('.'), i = s[0], v0 = !s[1]; | ||
return (i == 1 && v0 ) ? 'one' | ||
: ((i >= 2 && i <= 4) && v0 ) ? 'few' | ||
: (!v0 ) ? 'many' | ||
: 'other'; | ||
} | ||
> en = new MakePlural('en', {ordinals:1}) | ||
{ [Function] | ||
obj: | ||
{ lc: 'en', | ||
cardinals: true, | ||
ordinals: 1, | ||
parser: { n: 1, n10: 1, n100: 1, v0: 1 }, | ||
tests: { obj: [Circular], ordinal: [Object], cardinal: [Object] }, | ||
fn: [Circular] }, | ||
test: [Function], | ||
toString: [Function] } | ||
> en(2) | ||
> plurals.en(2) | ||
'other' | ||
> en(2, true) | ||
> plurals.en(2, true) // 2nd param, if true-ish, is for ordinal rules | ||
'two' | ||
> console.log(en.toString()) | ||
function(n, ord) { | ||
> console.log(plurals.en.toString()) | ||
function (n, ord) { | ||
var s = String(n).split('.'), v0 = !s[1], t0 = Number(s[0]) == n, | ||
@@ -106,12 +92,11 @@ n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2); | ||
### Precompiled use: Web | ||
## Usage: Web | ||
```html | ||
<script src="path/to/make-plural.browser.js"></script> | ||
<script src="path/to/make-plural/plurals.min.js"></script> | ||
<script> | ||
var ru = new MakePlural('ru', {ordinals:1}); | ||
console.log('1: ' + ru(1) + ', 3.0: ' + ru(3.0) + | ||
', "1.0": ' + ru('1.0') + ', "0": ' + ru('0')); | ||
console.log(ru.toString()); | ||
var ru = plurals.ru | ||
console.log('1: ' + plurals.ru(1) + ', 3.0: ' + plurals.ru(3.0) + | ||
', "1.0": ' + plurals.ru('1.0') + ', "0": ' + plurals.ru('0')); | ||
console.log(plurals.ru.toString()); | ||
</script> | ||
@@ -136,36 +121,11 @@ ``` | ||
The browser versions of MakePlural (`./make-plural.browser.js` and | ||
`./make-plural.min.js`) are compiled to include the default CLDR rules. If that | ||
doesn't work for you, you should either modify `src/browser.js` to fit your | ||
needs, or remove the `module.exports = ...` statement from `./make-plural.js` | ||
and use that, calling `MakePlural.load(cldr)` with your custom data. | ||
## Usage: CLI | ||
```sh | ||
$ ./bin/make-plural | ||
Locales verified ok: | ||
af ak am ar asa ast az be bem bez bg bh bm bn bo br brx bs ca cgg chr ckb | ||
cs cy da de dsb dv dz ee el en eo es et eu fa ff fi fil fo fr fur fy ga | ||
gd gl gsw gu guw gv ha haw he hi hr hsb hu hy id ig ii in is it iu iw ja | ||
jbo jgo ji jmc jv jw ka kab kaj kcg kde kea kk kkj kl km kn ko ks ksb ksh | ||
ku kw ky lag lb lg lkt ln lo lt lv mas mg mgo mk ml mn mo mr ms mt my nah | ||
naq nb nd ne nl nn nnh no nqo nr nso ny nyn om or os pa pap pl prg ps pt | ||
pt-PT rm ro rof root ru rwk sah saq se seh ses sg sh shi si sk sl sma smi | ||
smj smn sms sn so sq sr ss ssy st sv sw syr ta te teo th ti tig tk tl tn | ||
to tr ts tzm ug uk ur uz ve vi vo vun wa wae wo xh xog yi yo zh zu | ||
$ ./bin/make-plural fr | ||
function fr(n, ord) { | ||
if (ord) return (n == 1) ? 'one' : 'other'; | ||
return (n >= 0 && n < 2) ? 'one' : 'other'; | ||
} | ||
$ ./bin/make-plural fr 1.5 | ||
one | ||
Note that with `plurals.min.js`, the stringified function would be rendered as: | ||
```js | ||
function (e,t){var r=String(e).split("."),n=r[0],o=!r[1],c=n.slice(-1), | ||
i=n.slice(-2);return t?"other":o&&1==c&&11!=i?"one":o&&c>=2&&4>=c&&(12>i||i>14)? | ||
"few":o&&0==c||o&&c>=5&&9>=c||o&&i>=11&&14>=i?"many":"other"} | ||
``` | ||
## Methods | ||
## `make-plural.js` - Live compiler | ||
@@ -209,2 +169,86 @@ ### new MakePlural(lc, opt) | ||
### Live use: Node | ||
```js | ||
> MakePlural = require('make-plural/make-plural').load( | ||
... require('./data/plurals.json'), require('./data/ordinals.json')) | ||
{ [Function: MakePlural] | ||
cardinals: true, | ||
ordinals: false, | ||
rules: | ||
{ cardinal: | ||
{ af: [Object], | ||
ak: [Object], | ||
am: [Object], | ||
// snip 193 lines... | ||
yo: [Object], | ||
zh: [Object], | ||
zu: [Object] }, | ||
ordinal: | ||
{ af: [Object], | ||
am: [Object], | ||
ar: [Object], | ||
// snip 78 lines... | ||
vi: [Object], | ||
zh: [Object], | ||
zu: [Object] } } } | ||
> sk = new MakePlural('sk') // Note: not including ordinals by default | ||
{ [Function] | ||
obj: | ||
{ lc: 'sk', | ||
cardinals: true, | ||
ordinals: false, | ||
parser: { v0: 1, i: 1 }, | ||
tests: { obj: [Circular], ordinal: {}, cardinal: [Object] }, | ||
fn: [Circular] }, | ||
test: [Function], | ||
toString: [Function] } | ||
> sk(1) | ||
'one' | ||
> sk(3.0) | ||
'few' | ||
> sk('1.0') | ||
'many' | ||
> sk('0') | ||
'other' | ||
> console.log(sk.toString()) | ||
function(n) { | ||
var s = String(n).split('.'), i = s[0], v0 = !s[1]; | ||
return (i == 1 && v0 ) ? 'one' | ||
: ((i >= 2 && i <= 4) && v0 ) ? 'few' | ||
: (!v0 ) ? 'many' | ||
: 'other'; | ||
} | ||
``` | ||
`make-plural.js` may also be used in browser environments; see `test/index.html` | ||
for an example of its use. | ||
## CLI Usage | ||
```sh | ||
$ ./bin/make-plural fr | ||
function fr(n, ord) { | ||
if (ord) return (n == 1) ? 'one' : 'other'; | ||
return (n >= 0 && n < 2) ? 'one' : 'other'; | ||
} | ||
$ ./bin/make-plural fr 1.5 | ||
one | ||
$ ./bin/make-plural fr 1.5 true | ||
other | ||
``` | ||
Please see the source of `src/index.js` for more details. | ||
## Dependencies | ||
@@ -222,9 +266,9 @@ | ||
> cldr = require('cldr-data'); | ||
> MakePlural = require('make-plural').load( | ||
> MakePlural = require('make-plural/make-plural').load( | ||
cldr('supplemental/plurals'), | ||
cldr('supplemental/ordinals') | ||
); | ||
> ar = new MakePlural('ar'); | ||
> ar(3.14); | ||
'other' | ||
> en = new MakePlural('en'); | ||
> en(3, true) | ||
'few' | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
144580
10
2150
269
10
2
4