Skip to content

Commit

Permalink
rewrite for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Veneman committed Apr 14, 2024
1 parent 23c2e76 commit 0894061
Show file tree
Hide file tree
Showing 13 changed files with 6,985 additions and 161 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: NPM Publish

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install --ignore-scripts --no-audit
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm install --ignore-scripts --no-audit
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
48 changes: 48 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Tests

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
lint:
name: Lint JS
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Lint JS
run: npx --yes oxlint@latest -D perf
types:
name: Types
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20
cache: "npm"
- name: Install dependencies
run: npm install --ignore-scripts --no-audit
- name: Test types
run: npm run check
test:
name: Unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
node-version: 20
cache: "npm"
- name: Install dependencies
run: npm install --ignore-scripts --no-audit
- name: Run unit tests
run: npm test
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
package-lock.json
.nyc_output
dist
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

47 changes: 47 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Compare two CSS <time> values
* @param {string} a
* @param {string} b
* @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`
* @example
* compare('1s', '2s') // -1
* compare('1000ms', '1s') // 0
*/
export function compare(a, b) {
const A = convert(a)
const B = convert(b)

// If times are the same, put ms in front of s
if (A === B) {
return /\d+ms$/i.test(a) ? -1 : 1
}

return A - B
}

/**
* Convert a time string to ms
* @param {string} time
* @returns {number}
* @example
* convert('1s') // 1000
* convert('1ms') // 1
* convert('+2ms') // 2
* convert('var(--foo)') // Number.MAX_SAFE_INTEGER - 1
* convert('bars') // Number.MAX_SAFE_INTEGER
*/
export function convert(time) {
if (/\d+ms$/i.test(time)) {
return Number(time.slice(0, time.length - 2))
}

if (/\d+s$/i.test(time)) {
return Number(time.slice(0, time.length - 1)) * 1000
}

if (time.startsWith('var(')) {
return Number.MAX_SAFE_INTEGER - 1
}

return Number.MAX_SAFE_INTEGER
}
4 changes: 2 additions & 2 deletions license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Bart Veneman
Copyright (c) 2024 Bart Veneman

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

0 comments on commit 0894061

Please sign in to comment.