Comparing version 2.0.1 to 2.0.2
10
index.js
@@ -1,9 +0,7 @@ | ||
let locales = ['EN', 'DE']; | ||
const locales = require('./src/locales/index').list; | ||
let configLocale = 'EN'; | ||
const Timeconstruct = require('./src/classes/TimeConstruct'); | ||
const regex = require('./src/methods/regex'); | ||
const match = require('./src/methods/match'); | ||
const TimeConstruct = require('./src/classes/TimeConstruct'); | ||
const parse = require('./src/methods/parse'); | ||
const format = require('./src/methods/format'); | ||
const { UnsupportedLocaleError } = require('./src/errors'); | ||
const { UnsupportedLocaleError } = require('./src/errors'); | ||
let parsec = (time, config) => { | ||
@@ -27,3 +25,3 @@ return parse(time, config, configLocale); | ||
parsec.Timeconstruct = Timeconstruct; | ||
parsec.TimeConstruct = TimeConstruct; | ||
module.exports = parsec; |
{ | ||
"name": "parsec", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A duration parser and formatter to turn random garbage into program usable times.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node test.js", | ||
"benchmark": "node benchmark.js", | ||
"package": "npm run test & echo test complete & npm run benchmark" | ||
}, | ||
@@ -9,0 +11,0 @@ "repository": { |
@@ -0,2 +1,6 @@ | ||
const locales = require('../locales/index').list; | ||
const {UnsupportedLocaleError, UnsupportedCallbackTypeError} = require('../errors'); | ||
const format = require('../methods/format'); | ||
const maximise = require('../methods/maximise'); | ||
class TimeConstruct { | ||
@@ -7,3 +11,3 @@ constructor(ms = 0, seconds = 0, minutes = 0, hours = 0, days = 0, weeks = 0, months = 0, years = 0, raw, config) { | ||
format: false, | ||
locale:'EN' | ||
locale: 'EN' | ||
}; | ||
@@ -24,2 +28,3 @@ } | ||
} | ||
balance() { | ||
@@ -69,2 +74,3 @@ if (this.milliseconds >= 1000) { | ||
} | ||
toMS() { | ||
@@ -80,6 +86,46 @@ let ms = this.milliseconds; | ||
} | ||
format(string) { | ||
return format(string, this); | ||
format(string, locale = this.locale) { | ||
return format(string, this, locale); | ||
} | ||
into(string, locale = this.locale) { | ||
return maximise(string, this, locale); | ||
} | ||
setLocale(locale) { | ||
if (locales.includes(locale.toUpperCase())) { | ||
this.locale = locale.toUpperCase(); | ||
} else { | ||
throw new UnsupportedLocaleError(`${locale.toUpperCase()} is not a supported locale.`); | ||
} | ||
} | ||
awaitDuration(callback) { | ||
if (callback) { | ||
if (typeof callback === 'function') { | ||
setTimeout(() => { | ||
callback(); | ||
}, this.duration); | ||
} else { | ||
throw new UnsupportedCallbackTypeError('Callback should be a Function.') | ||
} | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, this.duration); | ||
}); | ||
} | ||
} | ||
sleep() { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, this.duration); | ||
}); | ||
} | ||
} | ||
module.exports = TimeConstruct; |
class UnsupportedLocaleError extends Error { | ||
constructor(text){ | ||
constructor(text) { | ||
super(text); | ||
@@ -9,4 +9,14 @@ this.name = this.constructor.name | ||
class UnsupportedCallbackTypeError extends Error { | ||
constructor(text) { | ||
super(text); | ||
this.name = this.constructor.name | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} | ||
module.exports = { | ||
UnsupportedLocaleError | ||
//U | ||
UnsupportedLocaleError, | ||
UnsupportedCallbackTypeError | ||
} |
module.exports = { | ||
EN: require('./locale-EN.js') | ||
list: ['EN', 'DE'], | ||
EN: require('./locale-EN'), | ||
DE: require('./locale-DE') | ||
} |
@@ -6,3 +6,3 @@ const regex = require('../methods/regex'); | ||
second: regex('(\\d+)[^a-z0-9]*?s(?:ec?)?', 'ig'), | ||
minute: regex('(\\d+)[^a-z0-9]*?m[^s^o](?:n?)?', 'ig'), | ||
minute: regex('(\\d+)[^a-z0-9]*?m[^so](?:n?)?', 'ig'), | ||
hour: regex('(\\d+)[^a-z0-9]*?h(?:our?)?', 'ig'), | ||
@@ -17,3 +17,3 @@ day: regex('(\\d+)[^a-z0-9]*?d(?:ay?)?', 'ig'), | ||
["seconds", regex('\{S\}|\{S(?:EC(?:OND(?:S?)?)?)\}', 'ig')], | ||
["minutes", regex('\{M\}|\{M(?:IN(?:UTE(?:S?)?)?)\}', 'ig')], | ||
["minutes", regex('\{M[^SO]\}|\{M(?:IN(?:UTE(?:S?)?)?)\}', 'ig')], | ||
["hours", regex('\{H\}|\{H(?:OUR(?:S?)?)\}', 'ig')], | ||
@@ -20,0 +20,0 @@ ["days", regex('\{D\}|\{D(?:AY(?:S?)?)\}', 'ig')], |
@@ -1,12 +0,7 @@ | ||
const regex = require('./regex'); | ||
module.exports = (parse, timeObject) => { | ||
parse = parse.replace(regex('\{MS\}|\{?MI(?:LLISECOND(?:S?)?)\}', 'ig'), timeObject.milliseconds); | ||
parse = parse.replace(regex('\{S\}|\{S(?:EC(?:OND(?:S?)?)?)\}', 'ig'), timeObject.seconds); | ||
parse = parse.replace(regex('\{M\}|\{M(?:IN(?:UTE(?:S?)?)?)\}', 'ig'), timeObject.minutes); | ||
parse = parse.replace(regex('\{H\}|\{H(?:OUR(?:S?)?)\}', 'ig'), timeObject.hours); | ||
parse = parse.replace(regex('\{D\}|\{D(?:AY(?:S?)?)\}', 'ig'), timeObject.days); | ||
parse = parse.replace(regex('\{W\}|\{W(?:EEK(?:S?)?)\}', 'ig'), timeObject.weeks); | ||
parse = parse.replace(regex('\{MO\}|\{MO(?:NTH(?:S?)?)\}', 'ig'), timeObject.months); | ||
parse = parse.replace(regex('\{Y\}|\{Y(?:EAR(?:S?)?)\}', 'ig'), timeObject.years); | ||
const locales = require('../locales/index'); | ||
module.exports = (parse, timeObject, locale) => { | ||
locales[locale].constants.forEach((constant, name) => { | ||
parse = parse.replace(constant, timeObject[name]); | ||
}); | ||
return parse; | ||
} |
47
test.js
const parsec = require('.'); | ||
parsec.setLocale('en') | ||
//parsec('1 week, 14 minutes, and 31 seconds'); | ||
//parsec.parse('1 second'); | ||
//parsec.parse('1s, 2seconds, 3 seconds, 4 secs, 1 sec') | ||
parsec.setLocale('en'); | ||
parsec('1 week, 14 minutes, and 31 seconds'); | ||
parsec.parse('1 second'); | ||
parsec.parse('1s, 2seconds, 3 seconds, 4 secs, 1 sec'); | ||
let time = parsec('1000ms 60s 60m 24h 7d 4w 12mo 10y', { | ||
format: false | ||
}); | ||
let formatted = parsec.format('There are {MS} Milliseconds in 1 second\n{SECONDS} seconds in 1 minute\n{MINUTES} minutes in 1 hour\n{HOURS} hours in 1 day\n{DAYS} days in 1 week\n{WEEKS} in 1 month\n{MONTHS} in 1 year\n{YEARS} in 1 decade', time); | ||
console.log(time); | ||
console.log(formatted); | ||
let formatted2 = time.format('There are {MS} Milliseconds in 1 second\n{SECONDS} seconds in 1 minute\n{MINUTES} minutes in 1 hour\n{HOURS} hours in 1 day\n{DAYS} days in 1 week\n{WEEKS} in 1 month\n{MONTHS} in 1 year\n{YEARS} in 1 decade'); | ||
//console.log(parsec(String(Number.MAX_SAFE_INTEGER+'Y')).format('{YEARS}')) | ||
console.log(parsec(String(Number.MAX_SAFE_INTEGER + 'Y')).format('{YEARS}')) | ||
time.setLocale('de'); | ||
console.log(time.format(`{MILLISEKUNDEN} ms {SEK} seconds {MIN} minutes {STUNDEN} hours {TAGE} days {WOCHEN} weeks {MONATE} months {JAHRE} years`)); | ||
console.log(time.into(`{MILLISEKUNDEN}`), time.duration); | ||
time.setLocale('en'); | ||
time = parsec('2s'); | ||
console.log("Waiting For 2 Seconds"); | ||
time.awaitDuration(() => { | ||
console.log('Waited For 2 Seconds'); | ||
sleepTest(); | ||
}); | ||
async function sleepTest() { | ||
console.log('Waiting for 2 Seconds'); | ||
await time.sleep(); | ||
console.log('Waited for 2 Seconds'); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
19601
15
375
1
0