Skip to content

Commit

Permalink
Stay updated with libs using
Browse files Browse the repository at this point in the history
  • Loading branch information
RoderickQiu committed May 10, 2020
1 parent 2b659df commit 35c2753
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ console.log(shi.ArabicNumberTimeParser(1, { lang: 'zh_CN', as: 'ms', to: 's', ro
console.log(shi.ArabicNumberTimeParser(NaN, { ignoreError: true }))//0 seconds (because errors ignored)

// output the version
console.log(shi.version());//0.4.1
console.log(shi.version());//0.4.2
```

## Features
Expand All @@ -54,7 +54,7 @@ console.log(shi.version());//0.4.1

### Time in Arabic number -> Human time

- Time with units `d, h, m (min), s, ms` to human time in languages including: `'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'fa', 'fi', 'fo', 'fr', 'hr', 'hu', 'id', 'is', 'it', 'ja', 'ko', 'lo', 'lt', 'lv', 'ms', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'uk', 'ur', 'sk', 'sv', 'tr', 'th', 'vi', 'zh_CN', 'zh_TW'`.
- Time with units `d, h, m (min), s, ms` to human time in languages including: `'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'fa', 'fi', 'fo', 'fr', 'he', 'hr', 'hu', 'id', 'is', 'it', 'ja', 'ko', 'lo', 'lt', 'lv', 'ms', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'uk', 'ur', 'sk', 'sv', 'tr', 'th', 'vi', 'zh_CN', 'zh_TW'`.

### Time unix translate

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "node-shi",
"version": "0.4.1",
"version": "0.4.2",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"dependencies": {
"chinese-numbers-converter": "^2.3.1",
"english2number": "^1.0.8",
"humanize-duration": "^3.21.0",
"humanize-duration": "^3.22.0",
"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 words.",
Expand All @@ -17,7 +17,7 @@
"email": "scrisqiu@hotmail.com",
"url": "http://github.com/RoderickQiu"
},
"homepage": "https://shi.r-q.name",
"homepage": "https://github.com/RoderickQiu/node-shi/",
"bugs": {
"url": "https://github.com/RoderickQiu/node-shi/issues",
"email": "scrisqiu@hotmail.com"
Expand Down
11 changes: 6 additions & 5 deletions src/shi.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@ exports.humanTimeParser = humanTimeParser;
*'ar', 'bg', 'ca', 'cs', 'da',
*'de', 'el', 'en', 'es', 'et',
*'fa', 'fi', 'fo', 'fr', 'hr',
*'hu', 'id', 'is', 'it', 'ja',
*'ko', 'lo', 'lt', 'lv', 'ms',
*'nl', 'no', 'pl', 'pt', 'ro',
*'ru', 'uk', 'ur', 'sk', 'sv',
*'tr', 'th', 'vi', 'zh_CN', 'zh_TW'
*'he', 'hu', 'id', 'is', 'it',
*'ja', 'ko', 'lo', 'lt', 'lv',
*'ms', 'nl', 'no', 'pl', 'pt',
*'ro', 'ru', 'uk', 'ur', 'sk',
*'sv', 'tr', 'th', 'vi', 'zh_CN',
*'zh_TW'
* @param {Number} number the time in Arabic number (If `as` is not defined in `option`, we'll parse the number as second in default.)
* @param {Object} option (optional) *{ ignoreError: boolean, lang: language, round: boolean (round the decimal or not), as: (d, h, m (min), s, ms), to: (d, h, m (min), s, ms) }*
*/
Expand Down
43 changes: 22 additions & 21 deletions src/timestring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* By: Michael Barrett
* Package: timestring
* Edited by: Roderick Qiu
* Parent version: 6.0.0
*/

/**
Expand Down Expand Up @@ -44,29 +45,29 @@ const UNIT_MAP = {
/**
* Parse a timestring
*
* @param {String} string
* @param {String} returnUnit
* @param {Object} opts
* @return {Number}
* @param {string} string
* @param {string} returnUnit
* @param {Object} opts
* @returns {number}
*/

function parseTimestring(string, returnUnit, opts) {
opts = Object.assign({}, DEFAULT_OPTS, opts || {})

let totalSeconds = 0
let unitValues = getUnitValues(opts)
let groups = string
const unitValues = getUnitValues(opts)
const groups = string
.toLowerCase()
.replace(/[^.\w+-]+/g, '')
.match(/[-+]?[0-9.]+[a-z]+/g)

if (groups === null) {
throw new Error(`The string [${string}] could not be parsed`)
throw new Error(`The string [${string}] could not be parsed by timestring`)
}

groups.forEach(group => {
let value = group.match(/[0-9.]+/g)[0]
let unit = group.match(/[a-z]+/g)[0]
const value = group.match(/[0-9.]+/g)[0]
const unit = group.match(/[a-z]+/g)[0]

totalSeconds += getSeconds(value, unit, unitValues)
})
Expand All @@ -86,7 +87,7 @@ function parseTimestring(string, returnUnit, opts) {
*/

function getUnitValues(opts) {
let unitValues = {
const unitValues = {
ms: 0.001,
s: 1,
m: 60,
Expand All @@ -104,27 +105,27 @@ function getUnitValues(opts) {
/**
* Get the key for a unit
*
* @param {String} unit
* @returns {String}
* @param {string} unit
* @returns {string}
*/

function getUnitKey(unit) {
for (let key of Object.keys(UNIT_MAP)) {
for (const key of Object.keys(UNIT_MAP)) {
if (UNIT_MAP[key].indexOf(unit) > -1) {
return key
}
}

throw new Error(`The unit [${unit}] is not supported`)
throw new Error(`The unit [${unit}] is not supported by timestring`)
}

/**
* Get the number of seconds for a value, based on the unit
*
* @param {Number} value
* @param {String} unit
* @param {number} value
* @param {string} unit
* @param {Object} unitValues
* @returns {Number}
* @returns {number}
*/

function getSeconds(value, unit, unitValues) {
Expand All @@ -134,12 +135,12 @@ function getSeconds(value, unit, unitValues) {
/**
* Convert a value from its existing unit to a new unit
*
* @param {Number} value
* @param {String} unit
* @param {number} value
* @param {string} unit
* @param {Object} unitValues
* @returns {Number}
* @returns {number}
*/

function convert(value, unit, unitValues) {
return value / unitValues[getUnitKey(unit)]
}
}
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ console.log(shi.ArabicNumberTimeParser(1, { lang: 'zh_CN', as: 'ms', to: 's', ro
console.log(shi.ArabicNumberTimeParser(NaN, { ignoreError: true }))//0 seconds (because errors ignored)

// output the version
console.log(shi.version());//0.4.1
console.log(shi.version());//0.4.2
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ english2number@^1.0.8:
resolved "https://registry.npm.taobao.org/english2number/download/english2number-1.0.8.tgz#7b83974bea539746b39ca831137c9feaf595c488"
integrity sha1-e4OXS+pTl0aznKgxE3yf6vWVxIg=

humanize-duration@^3.21.0:
version "3.21.0"
resolved "https://registry.npm.taobao.org/humanize-duration/download/humanize-duration-3.21.0.tgz#ae5dc7e67640770cbf6a8d03a5d1138d47c7ce38"
integrity sha1-rl3H5nZAdwy/ao0DpdETjUfHzjg=
humanize-duration@^3.22.0:
version "3.22.0"
resolved "https://registry.npm.taobao.org/humanize-duration/download/humanize-duration-3.22.0.tgz#b0b577e2071f231e69b71bfae25e1bb342ffb915"
integrity sha1-sLV34gcfIx5ptxv64l4bs0L/uRU=

js-pinyin@^0.1.9:
version "0.1.9"
Expand Down

0 comments on commit 35c2753

Please sign in to comment.