From 24bf2cf39f45d98829d7a4f02fd3926b1e799c2c Mon Sep 17 00:00:00 2001 From: RoderickQiu Date: Fri, 21 Feb 2020 19:37:32 +0800 Subject: [PATCH] Bugs fixed & project updated --- _config.yml | 4 +++- package.json | 42 +++++++++++++++++++++++++++++++++++------- src/shi.js | 18 +++++++++++------- test.js | 8 ++++---- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/_config.yml b/_config.yml index 2f7efbe..06baf47 100644 --- a/_config.yml +++ b/_config.yml @@ -1 +1,3 @@ -theme: jekyll-theme-minimal \ No newline at end of file +theme: jekyll-theme-minimal +title: node-shi +show_downloads: true \ No newline at end of file diff --git a/package.json b/package.json index c1fffbe..bbb966f 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,7 @@ { "name": "node-shi", "version": "0.2.0", - "description": "It's a lib which let you to transfer human time to computer-readable time and also to transfer computer-generated time to human time, available for English and Chinese.", "main": "index.js", - "author": { - "name": "Roderick Qiu", - "url": "http://github.com/RoderickQiu" - }, - "license": "MIT", "scripts": { "test": "node test.js" }, @@ -15,5 +9,39 @@ "chinese-numbers-converter": "^2.3.1", "english2number": "^1.0.8", "js-pinyin": "^0.1.9" - } + }, + "description": "It's a lib which let you to transfer human time to computer-readable time and also to transfer computer-generated time to human time, available for English and Chinese.", + "author": { + "name": "Roderick Qiu", + "email": "scrisqiu@hotmail.com", + "url": "http://github.com/RoderickQiu" + }, + "homepage": "https://shi.r-q.name", + "bugs": { + "url": "https://github.com/RoderickQiu/node-shi/issues", + "email": "scrisqiu@hotmail.com" + }, + "repository": { + "type": "git", + "url": "git://github.com/RoderickQiu/node-shi.git" + }, + "license": "MIT", + "keywords": [ + "node-shi", + "shi", + "time", + "duration", + "scris", + "RoderickQiu", + "human", + "semantic", + "computer", + "convert", + "converter", + "Chinese", + "English", + "language", + "timestamp", + "generate" + ] } \ No newline at end of file diff --git a/src/shi.js b/src/shi.js index 5b6d7b2..aed7e79 100644 --- a/src/shi.js +++ b/src/shi.js @@ -8,9 +8,9 @@ const timestring = require('./timestring/index') const ChineseNumber = require('chinese-numbers-converter'); let toNumber = require("english2number"); let pinyin = require('js-pinyin'); -pinyin.setOptions({ checkPolyphone: false, charCase: 0 }); +pinyin.setOptions({ checkPolyphone: false, charCase: 1 }); -const ENGLISH_NUMBER_REGEX = /(((?<=\s|^)(zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|million|billion|trillion|quadrillion|quintillion|sextillion|septillion|octillion|onillion|decillion)|(?<=zero|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|million|billion|trillion|quadrillion|quintillion|sextillion|septillion|octillion|onillion|decillion)(\s|-))+)/gi; +const ENGLISH_NUMBER_REGEX = /(((?<=\s|^)(zero|one|ten|hundred|thousand|million|billion|trillion|quadrillion|quintillion|sextillion|septillion|octillion|onillion|decillion|twenty|twelve|two|three|thirteen|thirty|fourteen|fourty|four|fifteen|fifty|five|sixty|sixteen|six|seventeen|seventy|seven|eighty|eighteen|eight|ninety|nineteen|nine)|((?<=zero|one|ten|hundred|thousand|million|billion|trillion|quadrillion|quintillion|sextillion|septillion|octillion|onillion|decillion|twenty|twelve|two|three|thirteen|thirty|fourteen|fourty|four|fifteen|fifty|five|sixty|sixteen|six|seventeen|seventy|seven|eighty|eighteen|eight|ninety|nineteen|nine)(\s{0,}|-|s)))+)/gi; const POSITIVE_ARABIC_NUMBER_REGEX = /^\d+(\.\d+)?$/; //non-negative floats & integers const NEGATIVE_ARABIC_NUMBER_REGEX = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //negative floats @@ -31,16 +31,20 @@ function englishConverter(str) { return str; } else { try { - str = str.replace(/-/gi, ' ').replace(/and/gi, '');//delete '-' and unnecessary 'and's - return str.replace( + let replaced = str.replace(/-/gi, ' ').replace(/s(?=\s)/gi, '').replace(/(?<=\s|^)(and|&)(?=\s|$)/gi, ' ').replace(/(?<=\s|^)(an|a)(?=\s|$)/gi, ' one ');//delete unnecessary things + return replaced.replace( ENGLISH_NUMBER_REGEX, match => { - return toNumber(match.replace(/[,\s|s](?=$)/i, ''));//delete blanks and unnecessary 's's in the end + try { + return toNumber(match.replace(/[,\ss]{0,}(?=$)/gi, ''));//delete blanks and unnecessary 's's in the end + } + catch { + return match; + } } );//find and replace and the english numbers } - catch (e) { - console.log(e); + catch { return str; } } diff --git a/test.js b/test.js index 04c6438..ecc8d9b 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ var shi = require('./index.js'); // human time -> time in Arabic number -console.log(shi.humanTimeParser("five hundreds and twenty one seconds")); -console.log(shi.humanTimeParser("1 day 52 min 16sec")); -console.log(shi.humanTimeParser("三十三分钟")); -console.log(shi.humanTimeParser("50", 'min')) \ No newline at end of file +console.log(shi.humanTimeParser("five hundreds and twenty one seconds"));//521 +console.log(shi.humanTimeParser("a day and 一 hour, 52 min 16sec"));//93136 +console.log(shi.humanTimeParser("三十三分钟"));//1980 +console.log(shi.humanTimeParser("50", 'min'))//3000 \ No newline at end of file