Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
RoderickQiu committed Feb 21, 2020
0 parents commit 5086193
Show file tree
Hide file tree
Showing 13 changed files with 568 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
176 changes: 176 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@

# Created by https://www.gitignore.io/api/node,macos,windows,visualstudiocode
# Edit at https://www.gitignore.io/?templates=node,macos,windows,visualstudiocode

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# rollup.js default build output
dist/

# Uncomment the public line if your project uses Gatsby
# https://nextjs.org/blog/next-9-1#public-directory-support
# https://create-react-app.dev/docs/using-the-public-folder/#docsNav
# public

# Storybook build outputs
.out
.storybook-out

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# Temporary folders
tmp/
temp/

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/node,macos,windows,visualstudiocode
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}\\index.js"
}
]
}
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "install",
"type": "shell",
"command": "yarn",
"group": "none"
},
{
"label": "autoclean",
"type": "shell",
"command": "yarn autoclean --force",
"group": "none"
}
]
}
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Roderick Qiu <scrisqiu@hotmail.com> (https://r-q.name)

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 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.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# node-shi

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.

## Features

### Human time -> time in Arabic number

- Chinese number + Chinese unix -> time in Arabic number as seconds
- Chinese number + English unix -> time in Arabic number as seconds
- Arabic number + Chinese unix -> time in Arabic number as seconds
- Arabic number + English unix -> time in Arabic number as seconds
- English number + Chinese unix -> time in Arabic number as seconds
- English number + English unix -> time in Arabic number as seconds
- Arabic number in different unixs *(h/min/s/ms)* -> time in Arabic number as seconds

## Packages Using

- js-pinyin, waterchestnet, MIT License.
- timestring, mike182uk, MIT License.
- chinese-numbers-converter, anton-bot, MIT License.
- english2number, TSavo, MIT License.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/****
* @module node-shi
* @copyright Roderick Qiu
*/

'use strict';

module.exports = require("./src/shi")
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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"
}
}
86 changes: 86 additions & 0 deletions src/shi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/***
* Global
*/

'use strict';

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 });

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 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

function isNumber(val) {
if (POSITIVE_ARABIC_NUMBER_REGEX.test(val) || NEGATIVE_ARABIC_NUMBER_REGEX.test(val)) {
return true;
} else {
return false;
}
}

/***
* Func 1: Human time -> time in Arabic number
*/

function englishConverter(str) {
if (isNumber(str)) {
return str;
} else {
try {
str = str.replace(/-/gi, ' ').replace(/and/gi, '');//delete '-' and unnecessary 'and's
return str.replace(
ENGLISH_NUMBER_REGEX,
match => {
return toNumber(match.replace(/[,\s|s](?=$)/i, ''));//delete blanks and unnecessary 's's in the end
}
);//find and replace and the english numbers
}
catch (e) {
console.log(e);
return str;
}
}
}

function unitConverter(str, treatNumberAs) {
switch (treatNumberAs) {
case "h":
return str * 3600;
case "min":
return str * 60;
case "s":
return str;
case "ms":
return str / 1000;
default:
return str;
}
}

/**
* human time parsed to Arabic number in seconds.
* @param {*} str The string or the time in different unixes.
* @param {String} treatNumberAs (optional) h, min, s, ms
* @returns {Number} The converted number
*/
function humanTimeParser(str, treatNumberAs) {
if (isNumber(str)) {//number only
if (treatNumberAs) return unitConverter(str, treatNumberAs);
else return str;
} else {
try {
str = pinyin.getFullChars(new ChineseNumber(englishConverter(str)).toArabicString());// to make it available for cn/en time
return timestring(str);
}
catch (e) {
console.log(e);
return;
}
}
}

exports.humanTimeParser = humanTimeParser;
Loading

0 comments on commit 5086193

Please sign in to comment.