Skip to content

Commit

Permalink
Adds linting, coverage and structure change
Browse files Browse the repository at this point in the history
  • Loading branch information
repraze committed Jun 29, 2019
1 parent ec12f70 commit 9570333
Show file tree
Hide file tree
Showing 9 changed files with 3,343 additions and 415 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Dependency directories
node_modules/
93 changes: 93 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"extends": ["eslint:recommended"],
"env": {
"browser": false,
"mocha": true,
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2017
},
"rules": {
"max-len": [
"error",
{
"code": 120,
"tabWidth": 4,
"ignoreComments": true,
"ignoreTrailingComments": true,
"ignoreUrls": true,
"ignoreStrings": true,
"ignoreTemplateLiterals": true,
"ignoreRegExpLiterals": true
}
],
"indent": ["error", 4],
"one-var": ["error", "never"],
"semi": ["error", "always"],
"semi-spacing": [
"error",
{
"before": false,
"after": true
}
],
"keyword-spacing": [
"error",
{
"before": false,
"after": false,
"overrides": {
"const": {"after": true},
"let": {"after": true},
"return": {"after": true, "before": true},
"import": {"after": true},
"export": {"after": true},
"from": {"before": true, "after": true},
"as": {"after": true, "before": true},
"of": {"after": true, "before": true},
"this": {"before": true},
"throw": {"after": true, "before": true},
"case": {"after": true},
"default": {"after": true, "before": true}
}
}
],
"key-spacing": [
"error",
{
"align": "value",
"beforeColon": false,
"afterColon": true
}
],
"space-before-function-paren": ["error", "never"],
"object-curly-spacing": ["error", "never"],
"array-bracket-spacing": ["error", "never"],
"arrow-spacing": ["error", {"before": false, "after": false}],
"arrow-body-style": ["error", "as-needed"],
"space-in-parens": [2, "never"],
"space-before-blocks": ["error", "never"],

"spaced-comment": "off",
"space-infix-ops": "error",
"no-undef": "error",
"no-unused-vars": [
"error",
{
"vars": "all",
"args": "none",
"varsIgnorePattern": "logger|assert"
}
],
"no-trailing-spaces": [
"error",
{
"skipBlankLines": true,
"ignoreComments": true
}
],
"no-console": "off"
}
}
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "node"
- "7"
- "6"
- "8"
script:
- npm run lint
- npm run coverage
after_success: npm run report
76 changes: 5 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,6 @@
'use strict';
/*!
* delta-time
* MIT Licensed
*/

var nanosecond = 1e-6,
microsecond = 1e-3,
second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24,
week = day * 7,
month = day * 30.44,
year = day * 365.25;

var units = (function(){
return [
{match : ['ms','milli','millis','millisecond','milliseconds'], val : 1},
{match : ['s','sec','secs','second','seconds'], val : second},
{match : ['m','min','mins','minute','minutes'], val : minute},
{match : ['h','hr','hrs','hour','hours'], val : hour},
{match : ['d','day','days'], val : day},
{match : ['w','wk','wks','week','weeks'], val : week},
{match : ['mo','mos','month','months'], val : month},
{match : ['y','yr','yrs','year','years'], val : year},
{match : ['μs','micro','micros','microsecond','microseconds'], val : microsecond},
{match : ['ns','nano','nanos','nanosecond','nanoseconds'], val : nanosecond}
].reduce(function(units, u){
u.match.forEach(function(name){
units[name] = u.val;
});
return units;
}, {});
}());

var match = /(\-?\s*(\.\d+|\d+(\.\d*)?))\s*[a-zμ]+/g;
var split = /(-?[^a-zμ\s]+)([^\s]+)/;
var strip = /\s+/g;

var isString = function(s){
return typeof s === 'string' || s instanceof String;
}

module.exports = function(dt, unit){
var divider = 1;
if(unit != undefined){
divider = isString(unit) ? units[unit.toLowerCase()] : null;
if(!divider){
throw new Error("Unit given as a second parameter is invalid");
}
}

if(!isNaN(+dt)){
return (+dt) / divider;
}

if(!isString(dt)){
return 0;
}

var list = dt.toLowerCase().match(match);
if(list){
return list.reduce(function(acc, val){
var infos = val.replace(strip, '').match(split);
var amount = parseFloat(infos[1], 10);
var unit = infos[2].toLowerCase();

var multipliyer = units[unit] || 0;

return acc + amount * multipliyer;
}, 0) / divider;
}

return 0;
}
exports = module.exports = require("./lib/delta-time");
74 changes: 74 additions & 0 deletions lib/delta-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"use strict";

var nanosecond = 1e-6;
var microsecond = 1e-3;
var second = 1000;
var minute = second * 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var month = day * 30.44;
var year = day * 365.25;

var units = (function(){
return [
{match: ["ms", "milli", "millis", "millisecond", "milliseconds"], val: 1},
{match: ["s", "sec", "secs", "second", "seconds"], val: second},
{match: ["m", "min", "mins", "minute", "minutes"], val: minute},
{match: ["h", "hr", "hrs", "hour", "hours"], val: hour},
{match: ["d", "day", "days"], val: day},
{match: ["w", "wk", "wks", "week", "weeks"], val: week},
{match: ["mo", "mos", "month", "months"], val: month},
{match: ["y", "yr", "yrs", "year", "years"], val: year},
{match: ["μs", "micro", "micros", "microsecond", "microseconds"], val: microsecond},
{match: ["ns", "nano", "nanos", "nanosecond", "nanoseconds"], val: nanosecond}
].reduce(function(units, u){
u.match.forEach(function(name){
units[name] = u.val;
});
return units;
}, {});
})();

var match = /(-?\s*(\.\d+|\d+(\.\d*)?))\s*[a-zμ]+/g;
var split = /(-?[^a-zμ\s]+)([^\s]+)/;
var strip = /\s+/g;

var isString = function(s){
return typeof s === "string" || s instanceof String;
};

module.exports = function(dt, unit){
var divider = 1;
if(unit != undefined){
divider = isString(unit) ? units[unit.toLowerCase()] : null;
if(!divider){
throw new Error("Unit given as a second parameter is invalid");
}
}

if(!isNaN(+dt)){
return +dt / divider;
}

if(!isString(dt)){
return 0;
}

var list = dt.toLowerCase().match(match);
if(list){
return (
list.reduce(function(acc, val){
var infos = val.replace(strip, "").match(split);
var amount = parseFloat(infos[1], 10);
var unit = infos[2].toLowerCase();

var multipliyer = units[unit] || 0;

return acc + amount * multipliyer;
}, 0) / divider
);
}

return 0;
};
Loading

0 comments on commit 9570333

Please sign in to comment.