ms-rb 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +135 -20
- data/lib/ms/rb/version.rb +1 -1
- data/lib/ms.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f6d761b6b9a5ff849e3c44d81391ad5e1eeb00c1c6acf0799c9204fc0bfae982
|
|
4
|
+
data.tar.gz: a3b9380fd07100958cfeb2a2a4923426c9a26abddd15a4968345526ca325b20c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b53326c6b08843126fb30ea50a429e13751ecde1e87f7e20bb06125b82965f73f725d9a2c4a2681b9b17f5669886915941b46bf2d37d642087d9ca19063b6bbc
|
|
7
|
+
data.tar.gz: c1d4085bcaee66e094febb257aa246c3fc5163d91f38866d690f3fc5d30c7a3219e750793cf1267c757fbd5b6f2da6ca1ec5176b4ca5230c7afd11a8cf1a8c33
|
data/README.md
CHANGED
|
@@ -1,39 +1,154 @@
|
|
|
1
1
|
# ms-rb
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
[](https://badge.fury.io/rb/ms-rb)
|
|
4
|
+

|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
A Ruby port of the popular [`ms`](https://github.com/vercel/ms) library from Vercel.
|
|
8
|
+
|
|
9
|
+
Convert human-friendly time strings like `"1h"`, `"2 days"`, `"3w"`, `"1.5mo"` into milliseconds — and convert milliseconds back into strings.
|
|
10
|
+
|
|
11
|
+
This gem aims to match the behavior of Vercel/ms, including:
|
|
12
|
+
|
|
13
|
+
- `ms("1h") -> 3600000`
|
|
14
|
+
- `ms(3600000) -> "1h"`
|
|
15
|
+
- Strict and non-strict parsing
|
|
16
|
+
- Long and short formatting
|
|
17
|
+
- Floating-point and negative values
|
|
18
|
+
- Full unit support (ms, s, m, h, d, w, mo, y)
|
|
19
|
+
|
|
20
|
+
---
|
|
6
21
|
|
|
7
22
|
## Installation
|
|
8
23
|
|
|
24
|
+
Add to your Gemfile:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
gem "ms-rb"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or install directly:
|
|
31
|
+
|
|
9
32
|
```bash
|
|
10
33
|
gem install ms-rb
|
|
11
34
|
```
|
|
12
|
-
|
|
35
|
+
|
|
36
|
+
Then require it:
|
|
13
37
|
|
|
14
38
|
```ruby
|
|
15
|
-
|
|
39
|
+
require "ms"
|
|
16
40
|
```
|
|
17
41
|
|
|
42
|
+
---
|
|
43
|
+
|
|
18
44
|
## Usage
|
|
19
45
|
|
|
46
|
+
The main entrypoint is:
|
|
47
|
+
|
|
20
48
|
```ruby
|
|
21
|
-
|
|
49
|
+
Ms.ms(value, long: false)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Parse a duration string → milliseconds
|
|
22
53
|
|
|
23
|
-
|
|
24
|
-
Ms
|
|
25
|
-
Ms
|
|
26
|
-
Ms
|
|
54
|
+
```ruby
|
|
55
|
+
Ms.ms("2h") # => 7200000
|
|
56
|
+
Ms.ms("1.5d") # => 129600000
|
|
57
|
+
Ms.ms("3 weeks") # => 1814400000
|
|
58
|
+
Ms.ms("-10ms") # => -10
|
|
59
|
+
Ms.ms("1 s") # => 1000
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Format milliseconds → duration string
|
|
63
|
+
|
|
64
|
+
Short form:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
Ms.ms(3600000) # => "1h"
|
|
68
|
+
Ms.ms(5400000) # => "2h"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Long form:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
Ms.ms(3600000, long: true) # => "1 hour"
|
|
75
|
+
Ms.ms(5400000, long: true) # => "2 hours"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Strict parsing
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
Ms.parse_strict("1h") # => 3600000
|
|
82
|
+
Ms.parse_strict("foo") # => NaN
|
|
27
83
|
```
|
|
28
84
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Supported Units
|
|
88
|
+
|
|
89
|
+
| Unit(s) | Meaning |
|
|
90
|
+
|----------------------------------------|--------------|
|
|
91
|
+
| `ms`, `msec`, `millisecond`, `milliseconds` | Milliseconds |
|
|
92
|
+
| `s`, `sec`, `second`, `seconds` | Seconds |
|
|
93
|
+
| `m`, `min`, `minute`, `minutes` | Minutes |
|
|
94
|
+
| `h`, `hr`, `hour`, `hours` | Hours |
|
|
95
|
+
| `d`, `day`, `days` | Days |
|
|
96
|
+
| `w`, `week`, `weeks` | Weeks |
|
|
97
|
+
| `mo`, `month`, `months` | Months |
|
|
98
|
+
| `y`, `yr`, `year`, `years` | Years |
|
|
99
|
+
|
|
100
|
+
Supports floats, negative values, and mixed case:
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
Ms.ms("1.5H") # => 5400000
|
|
104
|
+
Ms.ms("-.5h") # => -1800000
|
|
105
|
+
Ms.ms("53 YeArS") # => 1672552800000
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## API
|
|
111
|
+
|
|
112
|
+
### `Ms.ms(value, long: false)`
|
|
113
|
+
|
|
114
|
+
Main entrypoint. Accepts:
|
|
115
|
+
|
|
116
|
+
- `String` → returns milliseconds (`Integer` or `Float`)
|
|
117
|
+
- `Numeric` → returns formatted string (`"1h"` or `"1 hour"`)
|
|
118
|
+
|
|
119
|
+
Example:
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
Ms.ms("2h") # => 7200000
|
|
123
|
+
Ms.ms(7200000) # => "2h"
|
|
124
|
+
Ms.ms(7200000, long: true) # => "2 hours"
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### `Ms.parse(string)`
|
|
128
|
+
|
|
129
|
+
Non-strict parser.
|
|
130
|
+
Returns `NaN` for invalid input.
|
|
131
|
+
|
|
132
|
+
### `Ms.parse_strict(string)`
|
|
133
|
+
|
|
134
|
+
Strict parser.
|
|
135
|
+
Raises on invalid input types, returns `NaN` for malformed strings.
|
|
136
|
+
|
|
137
|
+
### `Ms.format(ms, long: false)`
|
|
138
|
+
|
|
139
|
+
Formats milliseconds directly.
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## Testing
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
bundle install
|
|
147
|
+
bundle exec rake test
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## License
|
|
38
153
|
|
|
39
|
-
|
|
154
|
+
MIT © Austin Miller
|
data/lib/ms/rb/version.rb
CHANGED
data/lib/ms.rb
CHANGED
|
@@ -6,8 +6,8 @@ require_relative "ms/rb"
|
|
|
6
6
|
module Ms
|
|
7
7
|
module_function
|
|
8
8
|
|
|
9
|
-
def ms(value, options
|
|
10
|
-
Rb.ms(value, options)
|
|
9
|
+
def ms(value, **options)
|
|
10
|
+
Rb.ms(value, **options)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def parse(str)
|
|
@@ -18,7 +18,7 @@ module Ms
|
|
|
18
18
|
Rb.parse_strict(str)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def format(ms, options
|
|
22
|
-
Rb.format(ms, options)
|
|
21
|
+
def format(ms, **options)
|
|
22
|
+
Rb.format(ms, **options)
|
|
23
23
|
end
|
|
24
24
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ms-rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Austin Miller
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2025-
|
|
10
|
+
date: 2025-12-01 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
description: Parse and format durations like '2h', '1.5d', '3w', '500ms' into milliseconds
|
|
13
13
|
and back.
|