Skip to content

Commit 0894061

Browse files
author
Bart Veneman
committed
rewrite for v2
1 parent 23c2e76 commit 0894061

File tree

13 files changed

+6985
-161
lines changed

13 files changed

+6985
-161
lines changed

.github/workflows/release.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3+
4+
name: NPM Publish
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 20
18+
- run: npm install --ignore-scripts --no-audit
19+
- run: npm test
20+
21+
publish-npm:
22+
needs: build
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: actions/setup-node@v4
27+
with:
28+
node-version: 20
29+
registry-url: https://registry.npmjs.org/
30+
- run: npm install --ignore-scripts --no-audit
31+
- run: npm run build
32+
- run: npm publish
33+
env:
34+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.github/workflows/test.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Tests
5+
6+
on:
7+
push:
8+
branches: [master]
9+
pull_request:
10+
branches: [master]
11+
12+
jobs:
13+
lint:
14+
name: Lint JS
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
- name: Lint JS
20+
run: npx --yes oxlint@latest -D perf
21+
types:
22+
name: Types
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Use Node.js 20
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 20
30+
cache: "npm"
31+
- name: Install dependencies
32+
run: npm install --ignore-scripts --no-audit
33+
- name: Test types
34+
run: npm run check
35+
test:
36+
name: Unit tests
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v4
40+
- name: Use Node.js 20
41+
uses: actions/setup-node@v4
42+
with:
43+
node-version: 20
44+
cache: "npm"
45+
- name: Install dependencies
46+
run: npm install --ignore-scripts --no-audit
47+
- name: Run unit tests
48+
run: npm test

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules
2-
package-lock.json
3-
.nyc_output
2+
dist

.travis.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Compare two CSS <time> values
3+
* @param {string} a
4+
* @param {string} b
5+
* @returns {number} 0 if equal, smaller than 0 if A is less than B, >0 if A is greater than B. `ms` will be sorted before `s`
6+
* @example
7+
* compare('1s', '2s') // -1
8+
* compare('1000ms', '1s') // 0
9+
*/
10+
export function compare(a, b) {
11+
const A = convert(a)
12+
const B = convert(b)
13+
14+
// If times are the same, put ms in front of s
15+
if (A === B) {
16+
return /\d+ms$/i.test(a) ? -1 : 1
17+
}
18+
19+
return A - B
20+
}
21+
22+
/**
23+
* Convert a time string to ms
24+
* @param {string} time
25+
* @returns {number}
26+
* @example
27+
* convert('1s') // 1000
28+
* convert('1ms') // 1
29+
* convert('+2ms') // 2
30+
* convert('var(--foo)') // Number.MAX_SAFE_INTEGER - 1
31+
* convert('bars') // Number.MAX_SAFE_INTEGER
32+
*/
33+
export function convert(time) {
34+
if (/\d+ms$/i.test(time)) {
35+
return Number(time.slice(0, time.length - 2))
36+
}
37+
38+
if (/\d+s$/i.test(time)) {
39+
return Number(time.slice(0, time.length - 1)) * 1000
40+
}
41+
42+
if (time.startsWith('var(')) {
43+
return Number.MAX_SAFE_INTEGER - 1
44+
}
45+
46+
return Number.MAX_SAFE_INTEGER
47+
}

license

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Bart Veneman
3+
Copyright (c) 2024 Bart Veneman
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

0 commit comments

Comments
 (0)