Skip to content

Commit 5086193

Browse files
committed
Initial commit
0 parents  commit 5086193

13 files changed

Lines changed: 568 additions & 0 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
2+
# Created by https://www.gitignore.io/api/node,macos,windows,visualstudiocode
3+
# Edit at https://www.gitignore.io/?templates=node,macos,windows,visualstudiocode
4+
5+
### macOS ###
6+
# General
7+
.DS_Store
8+
.AppleDouble
9+
.LSOverride
10+
11+
# Icon must end with two \r
12+
Icon
13+
14+
# Thumbnails
15+
._*
16+
17+
# Files that might appear in the root of a volume
18+
.DocumentRevisions-V100
19+
.fseventsd
20+
.Spotlight-V100
21+
.TemporaryItems
22+
.Trashes
23+
.VolumeIcon.icns
24+
.com.apple.timemachine.donotpresent
25+
26+
# Directories potentially created on remote AFP share
27+
.AppleDB
28+
.AppleDesktop
29+
Network Trash Folder
30+
Temporary Items
31+
.apdisk
32+
33+
### Node ###
34+
# Logs
35+
logs
36+
*.log
37+
npm-debug.log*
38+
yarn-debug.log*
39+
yarn-error.log*
40+
lerna-debug.log*
41+
42+
# Diagnostic reports (https://nodejs.org/api/report.html)
43+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
44+
45+
# Runtime data
46+
pids
47+
*.pid
48+
*.seed
49+
*.pid.lock
50+
51+
# Directory for instrumented libs generated by jscoverage/JSCover
52+
lib-cov
53+
54+
# Coverage directory used by tools like istanbul
55+
coverage
56+
*.lcov
57+
58+
# nyc test coverage
59+
.nyc_output
60+
61+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
62+
.grunt
63+
64+
# Bower dependency directory (https://bower.io/)
65+
bower_components
66+
67+
# node-waf configuration
68+
.lock-wscript
69+
70+
# Compiled binary addons (https://nodejs.org/api/addons.html)
71+
build/Release
72+
73+
# Dependency directories
74+
node_modules/
75+
jspm_packages/
76+
77+
# TypeScript v1 declaration files
78+
typings/
79+
80+
# TypeScript cache
81+
*.tsbuildinfo
82+
83+
# Optional npm cache directory
84+
.npm
85+
86+
# Optional eslint cache
87+
.eslintcache
88+
89+
# Optional REPL history
90+
.node_repl_history
91+
92+
# Output of 'npm pack'
93+
*.tgz
94+
95+
# Yarn Integrity file
96+
.yarn-integrity
97+
98+
# dotenv environment variables file
99+
.env
100+
.env.test
101+
102+
# parcel-bundler cache (https://parceljs.org/)
103+
.cache
104+
105+
# next.js build output
106+
.next
107+
108+
# nuxt.js build output
109+
.nuxt
110+
111+
# rollup.js default build output
112+
dist/
113+
114+
# Uncomment the public line if your project uses Gatsby
115+
# https://nextjs.org/blog/next-9-1#public-directory-support
116+
# https://create-react-app.dev/docs/using-the-public-folder/#docsNav
117+
# public
118+
119+
# Storybook build outputs
120+
.out
121+
.storybook-out
122+
123+
# vuepress build output
124+
.vuepress/dist
125+
126+
# Serverless directories
127+
.serverless/
128+
129+
# FuseBox cache
130+
.fusebox/
131+
132+
# DynamoDB Local files
133+
.dynamodb/
134+
135+
# Temporary folders
136+
tmp/
137+
temp/
138+
139+
### VisualStudioCode ###
140+
.vscode/*
141+
!.vscode/settings.json
142+
!.vscode/tasks.json
143+
!.vscode/launch.json
144+
!.vscode/extensions.json
145+
146+
### VisualStudioCode Patch ###
147+
# Ignore all local history of files
148+
.history
149+
150+
### Windows ###
151+
# Windows thumbnail cache files
152+
Thumbs.db
153+
Thumbs.db:encryptable
154+
ehthumbs.db
155+
ehthumbs_vista.db
156+
157+
# Dump file
158+
*.stackdump
159+
160+
# Folder config file
161+
[Dd]esktop.ini
162+
163+
# Recycle Bin used on file shares
164+
$RECYCLE.BIN/
165+
166+
# Windows Installer files
167+
*.cab
168+
*.msi
169+
*.msix
170+
*.msm
171+
*.msp
172+
173+
# Windows shortcuts
174+
*.lnk
175+
176+
# End of https://www.gitignore.io/api/node,macos,windows,visualstudiocode

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "launch",
7+
"name": "Launch Program",
8+
"program": "${workspaceRoot}\\index.js"
9+
}
10+
]
11+
}

.vscode/tasks.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "install",
8+
"type": "shell",
9+
"command": "yarn",
10+
"group": "none"
11+
},
12+
{
13+
"label": "autoclean",
14+
"type": "shell",
15+
"command": "yarn autoclean --force",
16+
"group": "none"
17+
}
18+
]
19+
}

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Roderick Qiu <scrisqiu@hotmail.com> (https://r-q.name)
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 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.

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# node-shi
2+
3+
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.
4+
5+
## Features
6+
7+
### Human time -> time in Arabic number
8+
9+
- Chinese number + Chinese unix -> time in Arabic number as seconds
10+
- Chinese number + English unix -> time in Arabic number as seconds
11+
- Arabic number + Chinese unix -> time in Arabic number as seconds
12+
- Arabic number + English unix -> time in Arabic number as seconds
13+
- English number + Chinese unix -> time in Arabic number as seconds
14+
- English number + English unix -> time in Arabic number as seconds
15+
- Arabic number in different unixs *(h/min/s/ms)* -> time in Arabic number as seconds
16+
17+
## Packages Using
18+
19+
- js-pinyin, waterchestnet, MIT License.
20+
- timestring, mike182uk, MIT License.
21+
- chinese-numbers-converter, anton-bot, MIT License.
22+
- english2number, TSavo, MIT License.

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/****
2+
* @module node-shi
3+
* @copyright Roderick Qiu
4+
*/
5+
6+
'use strict';
7+
8+
module.exports = require("./src/shi")

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "node-shi",
3+
"version": "0.2.0",
4+
"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.",
5+
"main": "index.js",
6+
"author": {
7+
"name": "Roderick Qiu",
8+
"url": "http://github.com/RoderickQiu"
9+
},
10+
"license": "MIT",
11+
"scripts": {
12+
"test": "node test.js"
13+
},
14+
"dependencies": {
15+
"chinese-numbers-converter": "^2.3.1",
16+
"english2number": "^1.0.8",
17+
"js-pinyin": "^0.1.9"
18+
}
19+
}

src/shi.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/***
2+
* Global
3+
*/
4+
5+
'use strict';
6+
7+
const timestring = require('./timestring/index')
8+
const ChineseNumber = require('chinese-numbers-converter');
9+
let toNumber = require("english2number");
10+
let pinyin = require('js-pinyin');
11+
pinyin.setOptions({ checkPolyphone: false, charCase: 0 });
12+
13+
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;
14+
const POSITIVE_ARABIC_NUMBER_REGEX = /^\d+(\.\d+)?$/; //non-negative floats & integers
15+
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
16+
17+
function isNumber(val) {
18+
if (POSITIVE_ARABIC_NUMBER_REGEX.test(val) || NEGATIVE_ARABIC_NUMBER_REGEX.test(val)) {
19+
return true;
20+
} else {
21+
return false;
22+
}
23+
}
24+
25+
/***
26+
* Func 1: Human time -> time in Arabic number
27+
*/
28+
29+
function englishConverter(str) {
30+
if (isNumber(str)) {
31+
return str;
32+
} else {
33+
try {
34+
str = str.replace(/-/gi, ' ').replace(/and/gi, '');//delete '-' and unnecessary 'and's
35+
return str.replace(
36+
ENGLISH_NUMBER_REGEX,
37+
match => {
38+
return toNumber(match.replace(/[,\s|s](?=$)/i, ''));//delete blanks and unnecessary 's's in the end
39+
}
40+
);//find and replace and the english numbers
41+
}
42+
catch (e) {
43+
console.log(e);
44+
return str;
45+
}
46+
}
47+
}
48+
49+
function unitConverter(str, treatNumberAs) {
50+
switch (treatNumberAs) {
51+
case "h":
52+
return str * 3600;
53+
case "min":
54+
return str * 60;
55+
case "s":
56+
return str;
57+
case "ms":
58+
return str / 1000;
59+
default:
60+
return str;
61+
}
62+
}
63+
64+
/**
65+
* human time parsed to Arabic number in seconds.
66+
* @param {*} str The string or the time in different unixes.
67+
* @param {String} treatNumberAs (optional) h, min, s, ms
68+
* @returns {Number} The converted number
69+
*/
70+
function humanTimeParser(str, treatNumberAs) {
71+
if (isNumber(str)) {//number only
72+
if (treatNumberAs) return unitConverter(str, treatNumberAs);
73+
else return str;
74+
} else {
75+
try {
76+
str = pinyin.getFullChars(new ChineseNumber(englishConverter(str)).toArabicString());// to make it available for cn/en time
77+
return timestring(str);
78+
}
79+
catch (e) {
80+
console.log(e);
81+
return;
82+
}
83+
}
84+
}
85+
86+
exports.humanTimeParser = humanTimeParser;

0 commit comments

Comments
 (0)