npm install timestring-unitThis library is used for converting time strings, e.g. 1 month, to an integer value relative to another unit.
``javascript
const tsUnit = require('timestring-unit');
tsUnit.parse('1 hour'); // 3600000
tsUnit.parseTo('1 hour', 'ms'); // 3600000
tsUnit.parseTo('1 minute', 'ms'); // 60000
tsUnit.parseTo('1min', 'ms'); // 60000
tsUnit.parseTo('1h 1min', 'ms'); // 3660000
`
You can require the module in several ways:
`javascript
const tsUnit = require('timestring-unit');
tsUnit.parseTo('1 hour'); // 3600000
tsUnit.parseTo('1 hour', 's'); // 3600
tsUnit.parseTo('1h 1min', 'ms'); // 3660000
`
Modifies the String prototype. NOT RECOMMENDED
`javascript
const tsUnit = require('timestring-unit/prototype');
'1 hour'.toTime(); // 3600000
'1 hour'.toTime('s'); // 3600
'1h 1min'.toTime('ms'); // 3660000
`
Using the custom interpolation feature of ES6:
`javascript
const tsUnit = require('timestring-unit/tag');
tsUnit 1 hour; // 3600000${'s'} 1 hour
tsUnit ; // 36001h 1min ${'ms'}
tsUnit ; // 3660000`
This library was created because I was sick of having a mix of millisecond-based and second-based integer values in my configuration files. For example, using Redis EXPIRE (which uses seconds) and setTimeout in the same project, you get a config file that looks like this:
`javascript`
// config.js
module.exports = {
redis: {
defaultTtl: 1,
},
stuff: {
sleepTime: 1000,
},
};
Which uses which unit?
Instead, with this library, you can have this:
`javascript1s ${'s'}
// config.js
const tsUnit = require('timestring-unit/tag');
module.exports = {
redis: {
defaultTtl: tsUnit ,1s ${'ms'}
},
stuff: {
sleepTime: tsUnit ,`
},
};
I find this more explicit.
You could also defer the unit conversing when calling the library, keeping only strings in your configuration file:
`javascript
// config.js
module.exports = {
redis: {
defaultTtl: '1s',
},
stuff: {
sleepTime: '1s',
},
};
// redis.js
const tsUnit = require('timestring-unit');
module.exports.setTtl = async (key, ttl) => {
return await whicheverRedisClient.exec(EXPIRE ${key} ${tsUnit.parseTo(ttl || config.defaultTtl, 'ms')});``
};
And BAM! unit conversion isn't tied to your configuration anymore, only where it's appropriate.
Note that I don't recommend this method because it may impact performance. You should rather "compile" your strings beforehand, at least for non-variable ones.