-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore:Migration from size-satisfies to workspaces
- Loading branch information
Showing
9 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: size-satisfies CI | ||
|
||
on: | ||
push: | ||
branches: master | ||
paths: | ||
- workspaces/size-satisfies/** | ||
pull_request: | ||
paths: | ||
- workspaces/size-satisfies/** | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: [18.x, 20.x] | ||
fail-fast: false | ||
steps: | ||
- name: Harden Runner | ||
uses: step-security/harden-runner@128a63446a954579617e875aaab7d2978154e969 # v2.4.0 | ||
with: | ||
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs | ||
|
||
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: Install dependencies | ||
run: npm install | ||
- name: Run tests | ||
run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 NodeSecure | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# `size-satisfies` | ||
|
||
[![version](https://img.shields.io/github/package-json/v/NodeSecure/Cli?filename=workspaces%2Fsize-satisfies%2Fpackage.json&style=for-the-badge)](https://www.npmjs.com/package/@nodesecure/size-satisfies) | ||
[![OpenSSF | ||
Scorecard](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli/badge?style=for-the-badge)](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli) | ||
[![mit](https://img.shields.io/github/license/NodeSecure/Cli?style=for-the-badge)](https://github.com/NodeSecure/cli/blob/master/workspaces/size-satisfies/LICENSE) | ||
![size](https://img.shields.io/github/languages/code-size/NodeSecure/size-satisfies?style=for-the-badge) | ||
[![build](https://img.shields.io/github/actions/workflow/status/NodeSecure/cli/size-satisfies.yml?style=for-the-badge)](https://github.com/NodeSecure/cli/actions?query=workflow%3A%22Size+Satisfies+CI%22) | ||
|
||
Same as SemVer.satisfies but for file size! | ||
|
||
## Requirements | ||
|
||
- [Node.js](https://nodejs.org/en/) v18 or higher | ||
|
||
## Getting Started | ||
|
||
This package is available in the Node Package Repository and can be easily installed with [npm](https://docs.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com). | ||
|
||
```bash | ||
$ npm i @nodesecure/size-satisfies | ||
# or | ||
$ yarn add @nodesecure/size-satisfies | ||
``` | ||
|
||
## Usage example | ||
|
||
```js | ||
import { strict } from "assert"; | ||
import sizeSatisfies from "size-satisfies"; | ||
|
||
const { strictEqual } = strict; | ||
|
||
strictEqual(sizeSatisfies(">= 45KB", "20MB"), true); | ||
strictEqual(sizeSatisfies("= 1MB", "1MB"), true); | ||
strictEqual(sizeSatisfies("= 1MB", 2000), false); | ||
``` | ||
|
||
The first argument of the `sizeSatisfies` method is the pattern with the operator + size. Available operators are `>=`, `<=`, `>`, `<`, `=`. | ||
|
||
## API | ||
|
||
### sizeSatisfies(pattern: string, size: number | string): boolean | ||
|
||
When the size is a string we convert it to a bytes number. When the argument is a number we consider the value as bytes. | ||
|
||
Invalid pattern will always return **false**. | ||
|
||
## Contributors ✨ | ||
|
||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
|
||
[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) | ||
|
||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
|
||
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): | ||
|
||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
<!-- prettier-ignore-start --> | ||
<!-- markdownlint-disable --> | ||
<table> | ||
<tbody> | ||
<tr> | ||
<td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/thomas-gentilhomme/"><img src="https://avatars.githubusercontent.com/u/4438263?v=4?s=100" width="100px;" alt="Gentilhomme"/><br /><sub><b>Gentilhomme</b></sub></a><br /><a href="https://github.com/NodeSecure/size-satisfies/commits?author=fraxken" title="Code">💻</a> <a href="https://github.com/NodeSecure/size-satisfies/commits?author=fraxken" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/size-satisfies/pulls?q=is%3Apr+reviewed-by%3Afraxken" title="Reviewed Pull Requests">👀</a> <a href="#security-fraxken" title="Security">🛡️</a> <a href="https://github.com/NodeSecure/size-satisfies/issues?q=author%3Afraxken" title="Bug reports">🐛</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Rossb0b"><img src="https://avatars.githubusercontent.com/u/39910164?v=4?s=100" width="100px;" alt="Nicolas Hallaert"/><br /><sub><b>Nicolas Hallaert</b></sub></a><br /><a href="https://github.com/NodeSecure/size-satisfies/commits?author=Rossb0b" title="Documentation">📖</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="http://tonygo.dev"><img src="https://avatars.githubusercontent.com/u/22824417?v=4?s=100" width="100px;" alt="Tony Gorez"/><br /><sub><b>Tony Gorez</b></sub></a><br /><a href="https://github.com/NodeSecure/size-satisfies/commits?author=tony-go" title="Code">💻</a> <a href="https://github.com/NodeSecure/size-satisfies/commits?author=tony-go" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/size-satisfies/pulls?q=is%3Apr+reviewed-by%3Atony-go" title="Reviewed Pull Requests">👀</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tekeuange23"><img src="https://avatars.githubusercontent.com/u/35274201?v=4?s=100" width="100px;" alt="tekeuange23"/><br /><sub><b>tekeuange23</b></sub></a><br /><a href="https://github.com/NodeSecure/size-satisfies/commits?author=tekeuange23" title="Documentation">📖</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/fabnguess"><img src="https://avatars.githubusercontent.com/u/72697416?v=4?s=100" width="100px;" alt="Kouadio Fabrice Nguessan"/><br /><sub><b>Kouadio Fabrice Nguessan</b></sub></a><br /><a href="#maintenance-fabnguess" title="Maintenance">🚧</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/clbcky"><img src="https://avatars.githubusercontent.com/u/113340767?v=4?s=100" width="100px;" alt="clbcky"/><br /><sub><b>clbcky</b></sub></a><br /><a href="https://github.com/NodeSecure/size-satisfies/commits?author=clbcky" title="Tests">⚠️</a></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<!-- markdownlint-restore --> | ||
<!-- prettier-ignore-end --> | ||
|
||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare function sizeSatisfies(pattern: string, size: number | string): boolean; | ||
|
||
export = sizeSatisfies; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Require Third-party Dependencies | ||
import bytes from "bytes"; | ||
|
||
// CONSTANTS | ||
const kOperators = { | ||
">=": (lh, rh) => lh >= rh, | ||
"<=": (lh, rh) => lh <= rh, | ||
">": (lh, rh) => lh > rh, | ||
"<": (lh, rh) => lh < rh, | ||
"=": (lh, rh) => lh === rh | ||
}; | ||
|
||
/** | ||
* @function sizeSatisfies | ||
* @param {!string} pattern | ||
* @param {!(number | string)} size | ||
* @returns {boolean} | ||
*/ | ||
function sizeSatisfies(pattern, size) { | ||
const localSize = typeof size === "number" ? size : bytes(size); | ||
const regexResult = /^(?<operator>[><=]{1,2})\s*(?<patternSize>.*)/g.exec(pattern); | ||
if (regexResult === null) { | ||
return false; | ||
} | ||
const { operator, patternSize } = regexResult.groups; | ||
|
||
return kOperators[operator](localSize, bytes(patternSize)); | ||
} | ||
|
||
export default sizeSatisfies; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "@nodesecure/size-satisfies", | ||
"version": "1.1.0", | ||
"description": "Same as SemVer.satisfies but for file size!", | ||
"exports": "./index.js", | ||
"scripts": { | ||
"lint": "eslint index.js", | ||
"test-only": "node --test test/", | ||
"test": "npm run lint && npm run test-only" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/NodeSecure/size-satisfies.git" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"size", | ||
"satisfies", | ||
"utility", | ||
"parse", | ||
"parser", | ||
"convert", | ||
"converter" | ||
], | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"author": "GENTILHOMME Thomas <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/NodeSecure/size-satisfies/issues" | ||
}, | ||
"homepage": "https://github.com/NodeSecure/size-satisfies#readme", | ||
"dependencies": { | ||
"bytes": "^3.1.2" | ||
}, | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { test } from "node:test"; | ||
import assert from "node:assert"; | ||
|
||
|
||
// Require Internal Dependencies | ||
import sizeSatisfies from "../index.js"; | ||
|
||
test("invalid operator always return false", () => { | ||
assert.strictEqual(sizeSatisfies("!! 45KB", "45KB"), false); | ||
}); | ||
|
||
test("assert sizeSatisfies", () => { | ||
assert.strictEqual(sizeSatisfies(">= 45KB", "20MB"), true); | ||
assert.strictEqual(sizeSatisfies("<= 45KB", "10B"), true); | ||
assert.strictEqual(sizeSatisfies("= 45KB", "45KB"), true); | ||
assert.strictEqual(sizeSatisfies("= 45KB", "46KB"), false); | ||
assert.strictEqual(sizeSatisfies("= 45KB", 2000), false); | ||
assert.strictEqual(sizeSatisfies("> 45KB", "46KB"), true); | ||
assert.strictEqual(sizeSatisfies("> 45KB", "45KB"), false); | ||
assert.strictEqual(sizeSatisfies("< 45KB", "44KB"), true); | ||
}); | ||
|
||
|