Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

timestring

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timestring - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

.travis.yml

35

package.json
{
"name" : "timestring",
"description" : "Parse a human readable time string into a time based value",
"homepage" : "https://github.com/mike182uk/timestring",
"keywords" : [
"util",
"functional",
"time"
],
"author" : "Michael David Barrett <mike182uk@gmail.com>",
"repository" : {
"type": "git",
"url": "git://github.com/mike182uk/timestring.git"
},
"main" : "timestring.js",
"version" : "1.0.2"
"name": "timestring",
"description": "Parse a human readable time string into a time based value",
"homepage": "https://github.com/mike182uk/timestring",
"author": "Michael David Barrett <mike182uk@gmail.com>",
"repository": {
"type": "git",
"url": "git://github.com/mike182uk/timestring.git"
},
"main": "timestring.js",
"version": "1.1.0",
"devDependencies": {
"chai": "^1.9.1",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-uglify": "^0.5.0",
"grunt-mocha-test": "^0.11.0",
"load-grunt-tasks": "^0.6.0",
"mocha": "^1.20.1",
"time-grunt": "^0.4.0"
}
}
#Timestring
[![Build Status](https://travis-ci.org/mike182uk/timestring.svg?branch=master)](https://travis-ci.org/mike182uk/timestring)
[![NPM](https://nodei.co/npm/timestring.png?downloads=true&stars=true)](https://nodei.co/npm/timestring/)
Attempts to parse a human readable time string into a time based value.

@@ -161,7 +165,14 @@

All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the `timestring.js` script on your page:
All you need to do to get timestring working in the browser is download / clone this repo and make sure you include the `dist/timestring.min.js` script on your page:
```html
<script src="<path-to-src>/timestring.js"></script>
<script src="<path-to-src>/timestring.min.js"></script>
```
Alternatively you can you use bower to manage this dependency for you:
```
bower install timestring --save
```
###Node

@@ -168,0 +179,0 @@

(function(){
"use strict";
"use strict";
var Timestring = function(settings) {
// default settings
var defaults = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12
};
var Timestring = function(settings) {
// default settings
var defaults = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12
};
// merge default settings with user settings
var settings = settings || {};
this.settings = {};
for (var property in defaults) { this.settings[property] = defaults[property]; }
for (var property in settings) { this.settings[property] = settings[property]; }
// merge default settings with user settings
settings = settings || {};
this.settings = defaults;
for (var s in settings) { this.settings[s] = settings[s]; }
// time units
this.units = {
s: ['s', 'sec', 'secs', 'second', 'seconds'],
m: ['m', 'min', 'mins', 'minute', 'minutes'],
h: ['h', 'hr', 'hrs', 'hour', 'hours'],
d: ['d', 'day', 'days'],
w: ['w', 'week', 'weeks'],
mth: ['mth', 'mths','month', 'months'],
y: ['y', 'yr', 'yrs', 'year', 'years']
};
// time units
this.units = {
s: ['s', 'sec', 'secs', 'second', 'seconds'],
m: ['m', 'min', 'mins', 'minute', 'minutes'],
h: ['h', 'hr', 'hrs', 'hour', 'hours'],
d: ['d', 'day', 'days'],
w: ['w', 'week', 'weeks'],
mth: ['mth', 'mths','month', 'months'],
y: ['y', 'yr', 'yrs', 'year', 'years']
};
// time unit seconds mappings
this.unitValues = {
s: 1,
m: 60,
h: 3600
};
// dynamic time unit seconds mappings
// these are dynamic based on the settings
this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h;
this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d;
this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w;
this.unitValues.y = this.settings.monthsPerYear * this.unitValues.w;
// time unit seconds mappings
this.unitValues = {
s: 1,
m: 60,
h: 3600
};
Timestring.prototype.parse = function(string, returnUnit) {
// reference to this
var self = this;
// dynamic time unit seconds mappings
// these are dynamic based on the settings
this.unitValues.d = this.settings.hoursPerDay * this.unitValues.h;
this.unitValues.w = this.settings.daysPerWeek * this.unitValues.d;
this.unitValues.mth = this.settings.weeksPerMonth * this.unitValues.w;
this.unitValues.y = this.settings.monthsPerYear * this.unitValues.mth;
};
// get unit key helper
function getUnitKey(unit) {
for (var key in self.units) {
for (var u in self.units[key]) {
if (unit === self.units[key][u]) {
return key;
}
}
}
Timestring.prototype.parse = function(string, returnUnit) {
// reference to this
var that = this;
// throw exception if invalid unit is passed
throw 'The unit [' + unit + '] is not supported by timestring';
// get unit key helper
function getUnitKey(unit) {
for (var k in that.units) {
for (var u in that.units[k]) {
if (unit === that.units[k][u]) {
return k;
}
}
}
// convert a value to a specific unit
function convert(value, unit) {
var baseValue = self.unitValues[getUnitKey(unit)];
return value / baseValue;
}
// throw error if invalid unit was passed
throw new Error('The unit [' + unit + '] is not supported by timestring');
}
// get a value in seconds based on a specific unit
function getSeconds(value, unit) {
var baseValue = self.unitValues[getUnitKey(unit)];
return value * baseValue;
}
// convert a value to a specific unit
function convert(value, unit) {
var baseValue = that.unitValues[getUnitKey(unit)];
// seconds counter
var totalSeconds = 0;
return value / baseValue;
}
// split string into groups and get total seconds for each group
var groups = string
.toLowerCase() // convert words to lower case
.replace(/[^\.\w+-]+/g, '') // remove white space
.match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups)
// get a value in seconds based on a specific unit
function getSeconds(value, unit) {
var baseValue = that.unitValues[getUnitKey(unit)];
if (groups !== null) {
for(var i = 0; i < groups.length; i++) {
var g = groups[i];
var value = g.match(/[0-9]+/g)[0];
var unit = g.match(/[a-z]+/g)[0];
return value * baseValue;
}
totalSeconds += getSeconds(value, unit);
}
}
// seconds counter
var totalSeconds = 0;
// return total, convert if needed
return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds;
}
// split string into groups and get total seconds for each group
var groups = string
.toLowerCase() // convert words to lower case
.replace(/[^\.\w+-]+/g, '') // remove white space
.match(/[-+]?[0-9]+[a-z]+/g); // match time groups (digit followed by time unit - i.e 5d 15m = 2 time groups)
// add convenience method to string prototype
String.prototype.parseTime = function (unit, settings) {
return (new Timestring(settings)).parse(this, unit);
}
if (groups !== null) {
for(var i = 0; i < groups.length; i++) {
var g = groups[i];
var value = g.match(/[0-9]+/g)[0];
var unit = g.match(/[a-z]+/g)[0];
// export Timestring object for either the browser or node
if (typeof module !== 'undefined' && module.exports) {
module.exports = Timestring;
totalSeconds += getSeconds(value, unit);
}
}
else {
this.Timestring = Timestring;
}
// return total, convert if needed
return (returnUnit) ? convert(totalSeconds, returnUnit) : totalSeconds;
};
// add convenience method to string prototype
String.prototype.parseTime = function (unit, settings) {
return (new Timestring(settings)).parse(this, unit);
};
// export Timestring object
if (typeof module !== 'undefined' && module.exports) {
module.exports = Timestring;
}
else {
this.Timestring = Timestring;
}
}).call(this);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc