stringtoms
v1.0.5
Published
Convert string to milliseconds
Downloads
28
Maintainers
Readme
stringtoms
Convert string to milliseconds
Use human readable ms expressions
Supports single expressions like "1s" and complex ones "1m 1s"
Install:
Install with npm
npm install stringtoms
Use standalone
<script src="https://cdn.jsdelivr.net/npm/stringtoms/standalone/stringtoms.js"></script>
or
<script src="https://cdn.jsdelivr.net/npm/stringtoms/standalone/stringtoms.min.js"></script>
Access the library:
Import
import { stringtoms } from 'stringtoms';
Require
const stringtoms = require('stringtoms');
Standalone
<script src="https://cdn.jsdelivr.net/npm/stringtoms/standalone/stringtoms.min.js"></script>
<script>
// global stringtoms variable exists
</script>
Basics:
Supported units
- milliseconds as 'ms', 'mil', 'milli', 'millisecond', 'milliseconds'
- seconds as 's', 'se', 'sec', 'second', 'seconds'
- minutes as 'm', 'mi', 'min', 'minute', 'minutes'
- hours as 'h', 'ho', 'hour', 'hours'
Single expressions
1h, 1m, 1s and so on...
import { stringtoms } from 'stringtoms';
stringtoms('1ms');
stringtoms('1s');
stringtoms('1m');
stringtoms('1h');
Complex expressions
1h 1m, 1m 1s and so on...
import { stringtoms } from 'stringtoms';
stringtoms('1s 1ms'); // means 1 second and 1 millisecond
stringtoms('1m 1s'); // means 1 minute and 1 second
stringtoms('1m 1m'); // means 1 minute and 1 minute
stringtoms('1h 1ms'); // means 1 hour and 1 millisecond
Auto correction
1h is equal to 1 h is equal to 1 hour
Any \W symbols are removed. Spaces are not needed.
import { stringtoms } from 'stringtoms';
stringtoms('1h') === stringtoms('1 h') === stringtoms('1 hour') === stringtoms('1#hour');
stringtoms('1m1s') === stringtoms('1 m 1 s') === stringtoms('1 m1 s') === stringtoms('1m 1s');
Auto complete
1h 1 is 1h 1m, 1 1m is 1h 1m.
Tries to complete left and right for missing units.
import { stringtoms } from 'stringtoms';
stringtoms('1h 1') === stringtoms('1 h 1m') === stringtoms('1 1m');
stringtoms('1 1s 1') === stringtoms('1 m 1 s 1 ms') === stringtoms('1m 1s 1ms');