├── test ├── packages └── ms_test.dart ├── lib ├── ms.dart └── src │ └── ms.dart ├── pubspec.yaml ├── tool └── run_tests.sh ├── .gitignore ├── README.md └── LICENSE /test/packages: -------------------------------------------------------------------------------- 1 | ../packages -------------------------------------------------------------------------------- /lib/ms.dart: -------------------------------------------------------------------------------- 1 | library ms; 2 | 3 | part 'src/ms.dart'; 4 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: ms 2 | version: 0.0.1 3 | author: Karan Goel 4 | description: Tiny milisecond conversion utility. 5 | homepage: https://github.com/karan/ms.dart 6 | documentation: https://github.com/karan/ms.dart 7 | -------------------------------------------------------------------------------- /tool/run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | DIR=$( cd $( dirname "${BASH_SOURCE[0]}" )/.. && pwd ) 6 | 7 | echo "Analyzing library for warnings or type errors" 8 | dart --checked $DIR/test/ms_test.dart 9 | 10 | echo -e "\n✓ OK" 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Don’t commit the following directories created by pub. 2 | build/ 3 | packages/ 4 | .buildlog 5 | 6 | # Or the files created by dart2js. 7 | *.dart.js 8 | *.dart.precompiled.js 9 | *.js_ 10 | *.js.deps 11 | *.js.map 12 | 13 | # Include when developing application packages. 14 | pubspec.lock 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ms.dart: miliseconds conversion utility 2 | 3 | [![Build Status](https://drone.io/github.com/karan/ms.dart/status.png)](https://drone.io/github.com/karan/ms.dart/latest) 4 | 5 | Port of [guille/ms.js](https://github.com/guille/ms.js). 6 | 7 | ## Install 8 | 9 | In your `pubspec.yaml` add: 10 | 11 | dependencies: 12 | ms: ">=0.0.1" 13 | 14 | Then run 15 | 16 | $ pub get 17 | 18 | ## Usage 19 | 20 | In your dart code, 21 | 22 | ```dart 23 | import 'package:typeof/typeof.dart'; 24 | ``` 25 | 26 | ```dart 27 | ms('2 days'); // 172800000 28 | ms('1d'); // 86400000 29 | ms('10h'); // 36000000 30 | ms('2.5 hrs'); // 9000000 31 | ms('2h'); // 7200000 32 | ms('1m'); // 60000 33 | ms('5s'); // 5000 34 | ms('100'); // 100 35 | ``` 36 | 37 | ```dart 38 | ms(60000); // "1m" 39 | ms(2 * 60000); // "2m" 40 | ms(ms('10 hours')); // "10h" 41 | ``` 42 | 43 | ```dart 44 | ms(60000, { 'long': true }); // "1 minute" 45 | ms(2 * 60000, { 'long': true }); // "2 minutes" 46 | ms(ms('10 hours'), { 'long': true }); // "10 hours" 47 | ``` 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Karan Goel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/ms_test.dart: -------------------------------------------------------------------------------- 1 | library ms_test; 2 | 3 | import 'package:ms/ms.dart'; 4 | 5 | 6 | void test(test_val, expected_val, [Map options]) { 7 | var val = ms(test_val, options); 8 | if (val != expected_val) { 9 | throw new Exception('ms($test_val): Expect: $expected_val, Got: $val'); 10 | } 11 | } 12 | 13 | void main() { 14 | // short string 15 | test('100', 100); 16 | test('1m', 60000); 17 | test('1h', 3600000); 18 | test('2d', 172800000); 19 | test('1s', 1000); 20 | test('100ms', 100); 21 | test('1.5h', 5400000); 22 | test('1 s', 1000); 23 | test('1.5H', 5400000); 24 | test('.5ms', .5); 25 | 26 | // long string 27 | test('53 milliseconds', 53); 28 | test('17 msecs', 17); 29 | test('1 sec', 1000); 30 | test('1 min', 60000); 31 | test('1 hr', 3600000); 32 | test('2 days', 172800000); 33 | test('1.5 hours', 5400000); 34 | 35 | // numbers with long 36 | test(500, '500 ms', { 'long': true }); 37 | test(1000, '1 second', { 'long': true }); 38 | test(1200, '1 second', { 'long': true }); 39 | test(10000, '10 seconds', { 'long': true }); 40 | test(60 * 1000, '1 minute', { 'long': true }); 41 | test(60 * 1200, '1 minute', { 'long': true }); 42 | test(60 * 10000, '10 minutes', { 'long': true }); 43 | test(60 * 60 * 1000, '1 hour', { 'long': true }); 44 | test(60 * 60 * 1200, '1 hour', { 'long': true }); 45 | test(60 * 60 * 10000, '10 hours', { 'long': true }); 46 | test(24 * 60 * 60 * 1000, '1 day', { 'long': true }); 47 | test(24 * 60 * 60 * 1200, '1 day', { 'long': true }); 48 | test(24 * 60 * 60 * 10000, '10 days', { 'long': true }); 49 | test(234234234, '3 days', { 'long': true }); 50 | 51 | // numbers 52 | test(500, '500ms'); 53 | test(1000, '1s'); 54 | test(10000, '10s'); 55 | test(60 * 1000, '1m'); 56 | test(60 * 10000, '10m'); 57 | test(60 * 60 * 1000, '1h'); 58 | test(60 * 60 * 10000, '10h'); 59 | test(24 * 60 * 60 * 1000, '1d'); 60 | test(24 * 60 * 60 * 10000, '10d'); 61 | test(234234234, '3d'); 62 | 63 | print('-------------------'); 64 | print('All tests complete.'); 65 | print('-------------------'); 66 | } 67 | -------------------------------------------------------------------------------- /lib/src/ms.dart: -------------------------------------------------------------------------------- 1 | part of ms; 2 | 3 | const int s = 1000; 4 | const int m = s * 60; 5 | const int h = m * 60; 6 | const int d = h * 24; 7 | const int y = d * 365; 8 | 9 | /** 10 | * Parse or format the given [val] 11 | */ 12 | ms(val, [Map options]) { 13 | if (options == null) options = new Map(); 14 | 15 | if (val.runtimeType == String) { 16 | return parse(val); 17 | } 18 | 19 | if (options['long'] == true) { 20 | return long(val); 21 | } else { 22 | return short(val); 23 | } 24 | } 25 | 26 | 27 | /** 28 | * Parse [str] and return the milliseconds 29 | */ 30 | num parse(String val) { 31 | val = val.toLowerCase(); 32 | RegExp tester = new RegExp(r'^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$'); 33 | Iterable match = tester.allMatches(val); 34 | 35 | if (match.length == 0) return -1; 36 | 37 | double n = double.parse(match.first.group(1)); 38 | String type = 'ms'; 39 | if (match.first.group(2) != null) { 40 | type = match.first.group(2).toLowerCase(); 41 | } 42 | 43 | switch (type) { 44 | case 'years': 45 | case 'year': 46 | case 'yrs': 47 | case 'yr': 48 | case 'y': 49 | return (n * y).toInt(); 50 | case 'days': 51 | case 'day': 52 | case 'd': 53 | return (n * d).toInt(); 54 | case 'hours': 55 | case 'hour': 56 | case 'hrs': 57 | case 'hr': 58 | case 'h': 59 | return (n * h).toInt(); 60 | case 'minutes': 61 | case 'minute': 62 | case 'mins': 63 | case 'min': 64 | case 'm': 65 | return (n * m).toInt(); 66 | case 'seconds': 67 | case 'second': 68 | case 'secs': 69 | case 'sec': 70 | case 's': 71 | return (n * s).toInt(); 72 | case 'milliseconds': 73 | case 'millisecond': 74 | case 'msecs': 75 | case 'msec': 76 | case 'ms': 77 | return n; 78 | } 79 | 80 | return -1; 81 | } 82 | 83 | 84 | /** 85 | * Short format for [ms] 86 | */ 87 | String short(int ms) { 88 | if (ms >= d) return (ms / d).round().toString() + 'd'; 89 | if (ms >= h) return (ms / h).round().toString() + 'h'; 90 | if (ms >= m) return (ms / m).round().toString() + 'm'; 91 | if (ms >= s) return (ms / s).round().toString() + 's'; 92 | return ms.toString() + 'ms'; 93 | } 94 | 95 | 96 | /** 97 | * Long format for [ms] 98 | */ 99 | String long(int ms) { 100 | 101 | String str = plural(ms, d, 'day'); 102 | if (str != '') return str; 103 | 104 | str = plural(ms, h, 'hour'); 105 | if (str != '') return str; 106 | 107 | str = plural(ms, m, 'minute'); 108 | if (str != '') return str; 109 | 110 | str = plural(ms, s, 'second'); 111 | if (str != '') return str; 112 | 113 | return ms.toString() + ' ms'; 114 | } 115 | 116 | 117 | // Pluralize for human readability 118 | String plural(int ms, n, name) { 119 | if (ms < n) return ''; 120 | if (ms < n * 1.5) return (ms / n).floor().toString() + ' ' + name; 121 | return (ms / n).ceil().toString() + ' ' + name + 's'; 122 | } 123 | --------------------------------------------------------------------------------