Skip to content

Commit

Permalink
Bugs fixed & project updated
Browse files Browse the repository at this point in the history
  • Loading branch information
RoderickQiu committed Feb 21, 2020
1 parent f101aad commit 24bf2cf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
4 changes: 3 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
theme: jekyll-theme-minimal
theme: jekyll-theme-minimal
title: node-shi
show_downloads: true
42 changes: 35 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
{
"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"
},
"dependencies": {
"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"
]
}
18 changes: 11 additions & 7 deletions src/shi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -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'))
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

0 comments on commit 24bf2cf

Please sign in to comment.