@phensley/plurals
Advanced tools
Comparing version 0.25.4 to 0.25.5
import { cardinalRules, expressions, ordinalRules } from './autogen.rules'; | ||
import { pluralRanges } from './autogen.ranges'; | ||
export { NumberOperands } from './operands'; | ||
@@ -16,3 +17,4 @@ import { PluralRules } from './rules'; | ||
var ordinals = resolve(ordinalRules, language, region); | ||
return new PluralRules(expressions, cardinals, ordinals); | ||
var ranges = pluralRanges[language] || pluralRanges.en; | ||
return new PluralRules(expressions, cardinals, ordinals, ranges); | ||
}; | ||
@@ -19,0 +21,0 @@ return Plurals; |
@@ -7,2 +7,5 @@ import { Decimal, DecimalArg } from '@phensley/decimal'; | ||
}; | ||
export declare type RangeMap = number | { | ||
[x: number]: number; | ||
}; | ||
export declare type Operand = 'n' | 'i' | 'v' | 'w' | 'f' | 't'; | ||
@@ -17,6 +20,8 @@ /** | ||
private ordinals; | ||
constructor(expressions: Expr[], cardinals: Rule[], ordinals: Rule[]); | ||
private ranges; | ||
constructor(expressions: Expr[], cardinals: Rule[], ordinals: Rule[], ranges: RangeMap); | ||
operands(d: Decimal): NumberOperands; | ||
cardinal(n: DecimalArg): string; | ||
ordinal(n: DecimalArg): string; | ||
range(start: DecimalArg, end: DecimalArg): string; | ||
private evaluate; | ||
@@ -23,0 +28,0 @@ private execute; |
@@ -5,2 +5,6 @@ import { coerceDecimal } from '@phensley/decimal'; | ||
var CATEGORIES = ['zero', 'one', 'two', 'few', 'many', 'other']; | ||
var arg = function (n) { | ||
return new NumberOperands(coerceDecimal(n)); | ||
}; | ||
var FLAG_OTHER = (5 << 5) + 5; | ||
/** | ||
@@ -11,6 +15,7 @@ * Set of all cardinal and ordinal plural rules, and the array of expression | ||
var PluralRules = /** @class */ (function () { | ||
function PluralRules(expressions, cardinals, ordinals) { | ||
function PluralRules(expressions, cardinals, ordinals, ranges) { | ||
this.expressions = expressions; | ||
this.cardinals = cardinals; | ||
this.ordinals = ordinals; | ||
this.ranges = ranges; | ||
} | ||
@@ -21,7 +26,16 @@ PluralRules.prototype.operands = function (d) { | ||
PluralRules.prototype.cardinal = function (n) { | ||
return this.evaluate(new NumberOperands(coerceDecimal(n)), this.cardinals); | ||
return CATEGORIES[this.evaluate(arg(n), this.cardinals)]; | ||
}; | ||
PluralRules.prototype.ordinal = function (n) { | ||
return this.evaluate(new NumberOperands(coerceDecimal(n)), this.ordinals); | ||
return CATEGORIES[this.evaluate(arg(n), this.ordinals)]; | ||
}; | ||
PluralRules.prototype.range = function (start, end) { | ||
if (typeof this.ranges === 'number') { | ||
return CATEGORIES[this.ranges]; | ||
} | ||
var s = this.evaluate(arg(start), this.cardinals); | ||
var e = this.evaluate(arg(end), this.cardinals); | ||
var cat = this.ranges[((1 << s) << 5) + (1 << e)]; | ||
return CATEGORIES[cat || this.ranges[FLAG_OTHER]]; | ||
}; | ||
PluralRules.prototype.evaluate = function (operands, rules) { | ||
@@ -31,6 +45,6 @@ for (var _i = 0, rules_1 = rules; _i < rules_1.length; _i++) { | ||
if (this.execute(operands, rule[1])) { | ||
return CATEGORIES[rule[0]]; | ||
return rule[0]; | ||
} | ||
} | ||
return 'other'; | ||
return 5; | ||
}; | ||
@@ -37,0 +51,0 @@ PluralRules.prototype.execute = function (operands, conditions) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var autogen_rules_1 = require("./autogen.rules"); | ||
var autogen_ranges_1 = require("./autogen.ranges"); | ||
var operands_1 = require("./operands"); | ||
@@ -19,3 +20,4 @@ exports.NumberOperands = operands_1.NumberOperands; | ||
var ordinals = resolve(autogen_rules_1.ordinalRules, language, region); | ||
return new rules_1.PluralRules(autogen_rules_1.expressions, cardinals, ordinals); | ||
var ranges = autogen_ranges_1.pluralRanges[language] || autogen_ranges_1.pluralRanges.en; | ||
return new rules_1.PluralRules(autogen_rules_1.expressions, cardinals, ordinals, ranges); | ||
}; | ||
@@ -22,0 +24,0 @@ return Plurals; |
@@ -7,2 +7,5 @@ import { Decimal, DecimalArg } from '@phensley/decimal'; | ||
}; | ||
export declare type RangeMap = number | { | ||
[x: number]: number; | ||
}; | ||
export declare type Operand = 'n' | 'i' | 'v' | 'w' | 'f' | 't'; | ||
@@ -17,6 +20,8 @@ /** | ||
private ordinals; | ||
constructor(expressions: Expr[], cardinals: Rule[], ordinals: Rule[]); | ||
private ranges; | ||
constructor(expressions: Expr[], cardinals: Rule[], ordinals: Rule[], ranges: RangeMap); | ||
operands(d: Decimal): NumberOperands; | ||
cardinal(n: DecimalArg): string; | ||
ordinal(n: DecimalArg): string; | ||
range(start: DecimalArg, end: DecimalArg): string; | ||
private evaluate; | ||
@@ -23,0 +28,0 @@ private execute; |
@@ -7,2 +7,6 @@ "use strict"; | ||
var CATEGORIES = ['zero', 'one', 'two', 'few', 'many', 'other']; | ||
var arg = function (n) { | ||
return new operands_1.NumberOperands(decimal_1.coerceDecimal(n)); | ||
}; | ||
var FLAG_OTHER = (5 << 5) + 5; | ||
/** | ||
@@ -13,6 +17,7 @@ * Set of all cardinal and ordinal plural rules, and the array of expression | ||
var PluralRules = /** @class */ (function () { | ||
function PluralRules(expressions, cardinals, ordinals) { | ||
function PluralRules(expressions, cardinals, ordinals, ranges) { | ||
this.expressions = expressions; | ||
this.cardinals = cardinals; | ||
this.ordinals = ordinals; | ||
this.ranges = ranges; | ||
} | ||
@@ -23,7 +28,16 @@ PluralRules.prototype.operands = function (d) { | ||
PluralRules.prototype.cardinal = function (n) { | ||
return this.evaluate(new operands_1.NumberOperands(decimal_1.coerceDecimal(n)), this.cardinals); | ||
return CATEGORIES[this.evaluate(arg(n), this.cardinals)]; | ||
}; | ||
PluralRules.prototype.ordinal = function (n) { | ||
return this.evaluate(new operands_1.NumberOperands(decimal_1.coerceDecimal(n)), this.ordinals); | ||
return CATEGORIES[this.evaluate(arg(n), this.ordinals)]; | ||
}; | ||
PluralRules.prototype.range = function (start, end) { | ||
if (typeof this.ranges === 'number') { | ||
return CATEGORIES[this.ranges]; | ||
} | ||
var s = this.evaluate(arg(start), this.cardinals); | ||
var e = this.evaluate(arg(end), this.cardinals); | ||
var cat = this.ranges[((1 << s) << 5) + (1 << e)]; | ||
return CATEGORIES[cat || this.ranges[FLAG_OTHER]]; | ||
}; | ||
PluralRules.prototype.evaluate = function (operands, rules) { | ||
@@ -33,6 +47,6 @@ for (var _i = 0, rules_1 = rules; _i < rules_1.length; _i++) { | ||
if (this.execute(operands, rule[1])) { | ||
return CATEGORIES[rule[0]]; | ||
return rule[0]; | ||
} | ||
} | ||
return 'other'; | ||
return 5; | ||
}; | ||
@@ -39,0 +53,0 @@ PluralRules.prototype.execute = function (operands, conditions) { |
{ | ||
"name": "@phensley/plurals", | ||
"version": "0.25.4", | ||
"version": "0.25.5", | ||
"description": "Plural rules engine", | ||
@@ -37,3 +37,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@phensley/decimal": "0.25.4", | ||
"@phensley/decimal": "0.25.5", | ||
"tslib": "1.10.x" | ||
@@ -67,3 +67,3 @@ }, | ||
}, | ||
"gitHead": "52cf835e9a4ddc35c4dc7eeeb9e9dc5cb57c5158" | ||
"gitHead": "4066c30580c5d4dffd9261c1a989aff283a899ea" | ||
} |
@phensley/plurals | ||
Standalone CLDR plural rules engine. | ||
Standalone CLDR plural rules engine. Supports cardinal, ordinal, and plural range calculations. | ||
@@ -5,0 +5,0 @@ ## Example |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
189785
45
2949
+ Added@phensley/decimal@0.25.5(transitive)
- Removed@phensley/decimal@0.25.4(transitive)
Updated@phensley/decimal@0.25.5