From 334c687fbbe9cffe55268c40a2d6e8f7ca5bd6ed Mon Sep 17 00:00:00 2001 From: nmussy Date: Mon, 29 Jul 2013 11:40:45 +0200 Subject: [PATCH] Version 0.1.3 German translation, bug fixes --- .gitignore | 1 + CHANGELOG.md | 8 ++- component.json | 9 ++-- dist/readint.js | 123 +++++++++++++++++++++----------------------- dist/readint.min.js | 46 ++++++++--------- lib/readint.js | 101 ++++++++++++++---------------------- locales/de.js | 14 +++++ locales/en.js | 2 +- locales/es.js | 2 +- locales/fr.js | 2 +- 10 files changed, 151 insertions(+), 157 deletions(-) create mode 100644 .gitignore create mode 100644 locales/de.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 104ab44..6f512c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,16 @@ Changelog --------- +### 0.1.3 July 29, 2013 + +* German translation. +* New locale feature: left-to-right numbering, for a given level. +* Bug, translation, and code style fixes. + ### 0.1.2 July 28, 2013 * Added a standalone JavaScript version, thanks to [component](https://github.com/component/component). -* Minor fix on the French translation +* Minor fix on the French translation. ### 0.1.1 July 28, 2013 diff --git a/component.json b/component.json index 876d9f7..fd01136 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "readint", "repo": "nmussy/readint", "description": "Localized written number parsing", - "version": "0.1.2", + "version": "0.1.3", "keywords": [ "written", "number", "parsing", "parse", "integer", "int", "localized", "english", "french"], "main": "bin/readint.js", "scripts": [ @@ -10,6 +10,9 @@ "lib/readint.js", "locales/en.js", "locales/es.js", - "locales/fr.js" - ] + "locales/fr.js", + "locales/de.js" + ], + "licence": "MIT", + "demo": "http://nmussy.github.io/readint/" } \ No newline at end of file diff --git a/dist/readint.js b/dist/readint.js index fa72522..ee891f0 100644 --- a/dist/readint.js +++ b/dist/readint.js @@ -1,18 +1,18 @@ /** * The MIT License (MIT) - * + * * Copyright (c) 2013 Jimmy Gaussen - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -20,7 +20,7 @@ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -;(function(){ + ;(function(){ /** * Require the given path. @@ -223,41 +223,40 @@ module.exports = require('../lib/readint.js'); }); require.register("readint/lib/readint.js", function(exports, require, module){ function readint(str, locale) { - if(!isNaN(parseInt(str))) - return parseInt(str); + if(!isNaN(global = parseInt(str))) + return global; if(typeof locale === 'undefined') locale = 'en'; + locale = require('../locales/' + locale); - var locale = require('../locales/' + locale); - var tokens = tokenizeString(str, locale); - - - if(tokens.length > 0) - return parseInteger(tokens); + if((tokens = tokenizeString(str, locale)).length > 0) + return parseInteger(tokens, locale); return -1; }; -function parseInteger(tokens) { +function parseInteger(tokens, locale) { if(tokens.length == 0) return -1; if(tokens.length == 1) return tokens[0].value; - var highestLevel = -1; - var highestLevelIndex = -1; - for(var i = 0; i < tokens.length; ++i) { - if(tokens[i].value > highestLevel) { - highestLevel = tokens[i].value; + var highestLevelIndex = -1, lValue, rValue; + for(var i = 0, highestLevel = -1; i < tokens.length; ++i) + if(tokens[i].level > highestLevel) { + highestLevel = tokens[i].level; highestLevelIndex = i; } - } - var highestLevelValue = tokens[highestLevelIndex].value; - var leftHandValue = parseInteger(tokens.slice(0, highestLevelIndex)); - var rightHandValue = parseInteger(tokens.slice(highestLevelIndex + 1, tokens.length)); + if((lValue = parseInteger(tokens.slice(0, highestLevelIndex))) == -1) + lValue = 1; + if((rValue = parseInteger(tokens.slice(highestLevelIndex + 1, tokens.length))) == -1) + rValue = 0; - return (leftHandValue == -1 ? 1 : leftHandValue) * highestLevelValue + (rightHandValue == -1 ? 0 : rightHandValue); + if(typeof locale.LTRlevels !== 'undefined' && locale.LTRlevels.indexOf(highestLevelIndex) != -1) + return lValue + tokens[highestLevelIndex].value + rValue; + + return lValue * tokens[highestLevelIndex].value + rValue; }; function tokenizeString(str, locale) { @@ -265,55 +264,35 @@ function tokenizeString(str, locale) { var keywords = getKeywords(locale.values); var result = []; - if(typeof locale.filters === 'undefined') { - for(var i = 0; ; ++i) { - if(typeof tokens[i] === 'undefined') - break; + if(typeof locale.filters === 'undefined') + locale.filters = []; - if(tokens[i].length == 0) { - tokens.splice(i--, 1); - continue; - } - for(var j = 0; ; ++j) - if(typeof keywords[j] === 'undefined') { - tokens.splice(i--, 1); - break; - } else if(tokens[i].toLowerCase() == keywords[j].string){ - result.push(keywords[j]); - break; - } + for(var i = 0; typeof tokens[i] !== 'undefined'; ++i) { + if((tokens[i] = tokens[i].toLowerCase()).length == 0) { + tokens.splice(i--, 1); + continue; } - } else - for(var i = 0; ; ++i) { - if(typeof tokens[i] === 'undefined') - break; - if(tokens[i].length == 0) { + for(var j = 0; j < locale.filters.length; ++j) + tokens[i] = tokens[i].replace(locale.filters[j][0], locale.filters[j][1]); + + for(var j = 0; ; ++j) + if(j >= keywords.length) { tokens.splice(i--, 1); - continue; + break; + } else if(tokens[i] == keywords[j].string) { + result.push(keywords[j]); + break; } - tokens[i] = tokens[i].toLowerCase(); - for(var j = 0; j < locale.filters.length; ++j) - tokens[i] = tokens[i].replace(locale.filters[j][0], locale.filters[j][1]); - for(var j = 0; ; ++j) - if(typeof keywords[j] === 'undefined') { - tokens.splice(i--, 1); - break; - } else if(tokens[i] == keywords[j].string) { - result.push(keywords[j]); - break; - } - } - + } return result; }; function getKeywords (values) { var keys = []; - for(var i = 0; i < values.length; ++i) { + for(var i = 0; i < values.length; ++i) for(var key in values[i]) keys.push({'string': key, 'value': values[i][key], 'level': i}); - } return keys; }; @@ -322,7 +301,7 @@ module.exports = readint; require.register("readint/locales/en.js", function(exports, require, module){ module.exports = { 'splitter' : /\s|\sand/, - 'filters' : [[/s$/,''], [/ies$/, 'y']], + 'filters' : [[/ies?$/, 'y'], [/s$/,'']], 'values': [ {"zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "eleven": 11, "twelve": 12, "thirteen": 13, "fourteen": 14, "fifteen": 15, "sixteen": 16, "seventeen": 17, "eighteen": 18, "nineteen": 19}, {"ten": 10, "twenty": 20, "thirty": 30, "forty": 40, "fifty": 50, "sixty": 60, "seventy": 70, "eighty": 80, "ninety": 90}, @@ -337,7 +316,7 @@ module.exports = { require.register("readint/locales/es.js", function(exports, require, module){ module.exports = { 'splitter' : /\s|\sy/, - 'filters' : [[/s$/,''], [/[á]/, 'a'], [/[é]/, 'e'], [/[ú]/, 'u'], [/[ó]/, 'o'], [/[ñ]/, 'n'], [/[í]/, 'i']], + 'filters' : [[/á/, 'a'], [/é/, 'e'], [/ú/, 'u'], [/ó/, 'o'], [/ñ/, 'n'], [/í/, 'i']], 'values': [ {"zero": 0, "uno": 1, "dos": 2, "tres": 3, "cuatro": 4, "cinco": 5, "seis": 6, "siete": 7, "ocho": 8, "nueve": 9, "once": 11, "doze": 12, "trece": 13, "catorce": 14, "quince": 15, "diecisies": 16, "diecisiete": 17, "dieciocho": 18, "diecinueve": 19, "veintiuno": 21, "veintidos": 22, "veintitres": 23, "veinticuatro": 24, "veinticinco": 25, "veintiseis": 26, "veintisiete": 27, "veintiocho": 28, "veintinueve": 29}, {"diez": 10, "veinte": 20, "treinta": 30, "cuarenta": 40, "cicuenta": 50, "sensta": 60, "setenta": 70, "ochenta": 80, "noventa": 90}, @@ -352,7 +331,7 @@ module.exports = { require.register("readint/locales/fr.js", function(exports, require, module){ module.exports = { 'splitter' : /\s|et|-(?=[^vingt|dix])/, - 'filters' : [[/s$/,''], [/[àâ]/, 'a'], [/[êéèë]/, 'e'], [/[ùûûü]/, 'u'], [/[ô]/, 'o'], [/[ç]/, 'c'], [/[îï]/, 'i']], + 'filters' : [[/s$/,''], [/[àâ]/, 'a'], [/[êéèë]/, 'e'], [/[ùûûü]/, 'u'], [/ô/, 'o'], [/ç/, 'c'], [/[îï]/, 'i']], 'values': [ {"zero": 0, "un": 1, "deux": 2, "troi": 3, "quatre": 4, "cinq": 5, "six": 6, "sept": 7, "huit": 8, "neuf": 9, "onze": 11, "douze": 12, "treize": 13, "quatorze": 14, "quinze": 15, "seize": 16}, {"dix": 10, "vingt": 20, "trente": 30, "quarente": 40, "cinquante": 50, "soixante": 60, "soixante-dix": 70, "quatre-vingt": 80, "quatre-vingt-dix": 90}, @@ -364,6 +343,22 @@ module.exports = { ] }; }); +require.register("readint/locales/de.js", function(exports, require, module){ +module.exports = { + 'splitter' : /-|(hundert|tausend|million|milliard|billion)|und(?!ert)|\s/, + 'filters' : [[/ä/, 'a'], [/ö/, 'o'], [/ü/, 'u'], [/ß/, 'ss'], [/[es][ns]?$/, '']], + 'LTRlevels' : [1], + 'values': [ + {"null": 0, "ein": 1, "zwei": 2, "drei": 3, "vier": 4, "fuenf": 5, "sech": 6, "sieben": 7, "acht": 8, "neun": 9, "elf": 11, "zwoelf": 12, "dreizehn": 13, "vierzehn": 14, "fuenfzehn": 15, "sechzehn": 16, "siebzehn": 17, "achtzehn": 18, "neunzehn": 19}, + {"zehn": 10, "zwanzig": 20, "dreissig": 30, "vierzig": 40, "fuenfzig": 50, "sechzig": 60, "siebzig": 70, "achtzig": 80, "neunzig": 90}, + {"hundert": 100}, + {"tausend": 1000}, + {"million": 1000000}, + {"milliard": 1000000000}, + {"billion": 1000000000000} + ] +}; +}); require.alias("readint/bin/readint.js", "readint/index.js");if (typeof exports == "object") { module.exports = require("readint"); } else if (typeof define == "function" && define.amd) { diff --git a/dist/readint.min.js b/dist/readint.min.js index a4de083..586c5e0 100644 --- a/dist/readint.min.js +++ b/dist/readint.min.js @@ -1,28 +1,24 @@ -!function(){function e(i,t,n){var r,o,s=e.resolve(i) -if(null==s)throw n=n||i,t=t||"root",r=Error('Failed to require "'+n+'" from "'+t+'"'),r.path=n,r.parent=t,r.require=!0,r -return o=e.modules[s],o.exports||(o.exports={},o.client=o.component=!0,o.call(this,o.exports,e.relative(s),o)),o.exports}e.modules={},e.aliases={},e.resolve=function(i){var t,n -for("/"===i.charAt(0)&&(i=i.slice(1)),t=[i,i+".js",i+".json",i+"/index.js",i+"/index.json"],n=0;n0?r(n):-1):parseInt(e)}function r(e){var i,t,n,o,s,l +for(e=e.split("/"),i=i.split("/"),n=0;n0?r(tokens,n):-1):global}function r(e,i){var n,t,s,l,o if(0==e.length)return-1 if(1==e.length)return e[0].value -for(i=-1,t=-1,n=0;ni&&(i=e[n].value,t=n) -return o=e[t].value,s=r(e.slice(0,t)),l=r(e.slice(t+1,e.length)),(-1==s?1:s)*o+(-1==l?0:l)}function o(e,i){var t,n,r=e.split(i.splitter),o=s(i.values),l=[] -if(void 0===i.filters)for(t=0;void 0!==r[t];++t)if(0!=r[t].length)for(n=0;;++n){if(void 0===o[n]){r.splice(t--,1) -break}if(r[t].toLowerCase()==o[n].string){l.push(o[n]) -break}}else r.splice(t--,1) -else for(t=0;void 0!==r[t];++t)if(0!=r[t].length){for(r[t]=r[t].toLowerCase(),n=0;no&&(o=e[l].level,s=l) +return-1==(n=r(e.slice(0,s)))&&(n=1),-1==(t=r(e.slice(s+1,e.length)))&&(t=0),void 0!==i.LTRlevels&&-1!=i.LTRlevels.indexOf(s)?n+e[s].value+t:n*e[s].value+t}function s(e,i){var n,t,r=e.split(i.splitter),s=l(i.values),o=[] +for(void 0===i.filters&&(i.filters=[]),n=0;void 0!==r[n];++n)if(0!=(r[n]=r[n].toLowerCase()).length){for(t=0;t=s.length){r.splice(n--,1) +break}if(r[n]==s[t].string){o.push(s[t]) +break}}}else r.splice(n--,1) +return o}function l(e){var i,n,t=[] +for(i=0;i 0) - return parseInteger(tokens); + if((tokens = tokenizeString(str, locale)).length > 0) + return parseInteger(tokens, locale); return -1; }; -function parseInteger(tokens) { +function parseInteger(tokens, locale) { if(tokens.length == 0) return -1; if(tokens.length == 1) return tokens[0].value; - var highestLevel = -1; - var highestLevelIndex = -1; - for(var i = 0; i < tokens.length; ++i) { - if(tokens[i].value > highestLevel) { - highestLevel = tokens[i].value; + var highestLevelIndex = -1, lValue, rValue; + for(var i = 0, highestLevel = -1; i < tokens.length; ++i) + if(tokens[i].level > highestLevel) { + highestLevel = tokens[i].level; highestLevelIndex = i; } - } - var highestLevelValue = tokens[highestLevelIndex].value; - var leftHandValue = parseInteger(tokens.slice(0, highestLevelIndex)); - var rightHandValue = parseInteger(tokens.slice(highestLevelIndex + 1, tokens.length)); + if((lValue = parseInteger(tokens.slice(0, highestLevelIndex))) == -1) + lValue = 1; + if((rValue = parseInteger(tokens.slice(highestLevelIndex + 1, tokens.length))) == -1) + rValue = 0; + + if(typeof locale.LTRlevels !== 'undefined' && locale.LTRlevels.indexOf(highestLevelIndex) != -1) + return lValue + tokens[highestLevelIndex].value + rValue; - return (leftHandValue == -1 ? 1 : leftHandValue) * highestLevelValue + (rightHandValue == -1 ? 0 : rightHandValue); + return lValue * tokens[highestLevelIndex].value + rValue; }; function tokenizeString(str, locale) { var tokens = str.split(locale.splitter); var keywords = getKeywords(locale.values); - + var result = []; - if(typeof locale.filters === 'undefined') { - for(var i = 0; ; ++i) { - if(typeof tokens[i] === 'undefined') - break; + if(typeof locale.filters === 'undefined') + locale.filters = []; - if(tokens[i].length == 0) { - tokens.splice(i--, 1); - continue; - } - for(var j = 0; ; ++j) - if(typeof keywords[j] === 'undefined') { - tokens.splice(i--, 1); - break; - } else if(tokens[i].toLowerCase() == keywords[j].string){ - result.push(keywords[j]); - break; - } + for(var i = 0; typeof tokens[i] !== 'undefined'; ++i) { + if((tokens[i] = tokens[i].toLowerCase()).length == 0) { + tokens.splice(i--, 1); + continue; } - } else - for(var i = 0; ; ++i) { - if(typeof tokens[i] === 'undefined') - break; - if(tokens[i].length == 0) { + for(var j = 0; j < locale.filters.length; ++j) + tokens[i] = tokens[i].replace(locale.filters[j][0], locale.filters[j][1]); + + for(var j = 0; ; ++j) + if(j >= keywords.length) { tokens.splice(i--, 1); - continue; + break; + } else if(tokens[i] == keywords[j].string) { + result.push(keywords[j]); + break; } - tokens[i] = tokens[i].toLowerCase(); - for(var j = 0; j < locale.filters.length; ++j) - tokens[i] = tokens[i].replace(locale.filters[j][0], locale.filters[j][1]); - for(var j = 0; ; ++j) - if(typeof keywords[j] === 'undefined') { - tokens.splice(i--, 1); - break; - } else if(tokens[i] == keywords[j].string) { - result.push(keywords[j]); - break; - } - } - + } return result; }; function getKeywords (values) { var keys = []; - for(var i = 0; i < values.length; ++i) { + for(var i = 0; i < values.length; ++i) for(var key in values[i]) keys.push({'string': key, 'value': values[i][key], 'level': i}); - } return keys; }; diff --git a/locales/de.js b/locales/de.js new file mode 100644 index 0000000..a1c27c9 --- /dev/null +++ b/locales/de.js @@ -0,0 +1,14 @@ +module.exports = { + 'splitter' : /-|(hundert|tausend|million|milliard|billion)|und(?!ert)|\s/, + 'filters' : [[/ä/, 'a'], [/ö/, 'o'], [/ü/, 'u'], [/ß/, 'ss'], [/[es][ns]?$/, '']], + 'LTRlevels' : [1], + 'values': [ + {"null": 0, "ein": 1, "zwei": 2, "drei": 3, "vier": 4, "fuenf": 5, "sech": 6, "sieben": 7, "acht": 8, "neun": 9, "elf": 11, "zwoelf": 12, "dreizehn": 13, "vierzehn": 14, "fuenfzehn": 15, "sechzehn": 16, "siebzehn": 17, "achtzehn": 18, "neunzehn": 19}, + {"zehn": 10, "zwanzig": 20, "dreissig": 30, "vierzig": 40, "fuenfzig": 50, "sechzig": 60, "siebzig": 70, "achtzig": 80, "neunzig": 90}, + {"hundert": 100}, + {"tausend": 1000}, + {"million": 1000000}, + {"milliard": 1000000000}, + {"billion": 1000000000000} + ] +}; \ No newline at end of file diff --git a/locales/en.js b/locales/en.js index 2bb93d9..3a77b29 100644 --- a/locales/en.js +++ b/locales/en.js @@ -1,6 +1,6 @@ module.exports = { 'splitter' : /\s|\sand/, - 'filters' : [[/s$/,''], [/ies$/, 'y']], + 'filters' : [[/ies?$/, 'y'], [/s$/,'']], 'values': [ {"zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "eleven": 11, "twelve": 12, "thirteen": 13, "fourteen": 14, "fifteen": 15, "sixteen": 16, "seventeen": 17, "eighteen": 18, "nineteen": 19}, {"ten": 10, "twenty": 20, "thirty": 30, "forty": 40, "fifty": 50, "sixty": 60, "seventy": 70, "eighty": 80, "ninety": 90}, diff --git a/locales/es.js b/locales/es.js index edbf3cf..272efd7 100644 --- a/locales/es.js +++ b/locales/es.js @@ -1,6 +1,6 @@ module.exports = { 'splitter' : /\s|\sy/, - 'filters' : [[/s$/,''], [/[á]/, 'a'], [/[é]/, 'e'], [/[ú]/, 'u'], [/[ó]/, 'o'], [/[ñ]/, 'n'], [/[í]/, 'i']], + 'filters' : [[/á/, 'a'], [/é/, 'e'], [/ú/, 'u'], [/ó/, 'o'], [/ñ/, 'n'], [/í/, 'i']], 'values': [ {"zero": 0, "uno": 1, "dos": 2, "tres": 3, "cuatro": 4, "cinco": 5, "seis": 6, "siete": 7, "ocho": 8, "nueve": 9, "once": 11, "doze": 12, "trece": 13, "catorce": 14, "quince": 15, "diecisies": 16, "diecisiete": 17, "dieciocho": 18, "diecinueve": 19, "veintiuno": 21, "veintidos": 22, "veintitres": 23, "veinticuatro": 24, "veinticinco": 25, "veintiseis": 26, "veintisiete": 27, "veintiocho": 28, "veintinueve": 29}, {"diez": 10, "veinte": 20, "treinta": 30, "cuarenta": 40, "cicuenta": 50, "sensta": 60, "setenta": 70, "ochenta": 80, "noventa": 90}, diff --git a/locales/fr.js b/locales/fr.js index 2ced9e5..5081b54 100644 --- a/locales/fr.js +++ b/locales/fr.js @@ -1,6 +1,6 @@ module.exports = { 'splitter' : /\s|et|-(?=[^vingt|dix])/, - 'filters' : [[/s$/,''], [/[àâ]/, 'a'], [/[êéèë]/, 'e'], [/[ùûûü]/, 'u'], [/[ô]/, 'o'], [/[ç]/, 'c'], [/[îï]/, 'i']], + 'filters' : [[/s$/,''], [/[àâ]/, 'a'], [/[êéèë]/, 'e'], [/[ùûûü]/, 'u'], [/ô/, 'o'], [/ç/, 'c'], [/[îï]/, 'i']], 'values': [ {"zero": 0, "un": 1, "deux": 2, "troi": 3, "quatre": 4, "cinq": 5, "six": 6, "sept": 7, "huit": 8, "neuf": 9, "onze": 11, "douze": 12, "treize": 13, "quatorze": 14, "quinze": 15, "seize": 16}, {"dix": 10, "vingt": 20, "trente": 30, "quarente": 40, "cinquante": 50, "soixante": 60, "soixante-dix": 70, "quatre-vingt": 80, "quatre-vingt-dix": 90},