Skip to content

Commit 52b9fb3

Browse files
committed
chore:Migration from size-satisfies to workspaces
1 parent 3b8eb0d commit 52b9fb3

File tree

8 files changed

+241
-1
lines changed

8 files changed

+241
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: master
6+
paths:
7+
- workspaces/size-satisfies/**
8+
pull_request:
9+
paths:
10+
- workspaces/size-satisfies/**
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
test:
17+
runs-on: ubuntu-latest
18+
strategy:
19+
matrix:
20+
node-version: [18.x, 20.x]
21+
fail-fast: false
22+
steps:
23+
- name: Harden Runner
24+
uses: step-security/harden-runner@128a63446a954579617e875aaab7d2978154e969 # v2.4.0
25+
with:
26+
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs
27+
28+
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
29+
- name: Use Node.js ${{ matrix.node-version }}
30+
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
31+
with:
32+
node-version: ${{ matrix.node-version }}
33+
- name: Install dependencies
34+
run: npm install
35+
- name: Run tests
36+
run: npm run test

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
],
2929
"workspaces": [
3030
"workspaces/documentation-ui",
31-
"workspaces/vis-network"
31+
"workspaces/vis-network",
32+
"workspaces/size-satisfies"
3233
],
3334
"repository": {
3435
"type": "git",

workspaces/size-satisfies/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 NodeSecure
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.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# `size-satisfies`
2+
3+
[![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)
4+
[![OpenSSF
5+
Scorecard](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli/badge?style=for-the-badge)](https://api.securityscorecards.dev/projects/github.com/NodeSecure/cli)
6+
[![mit](https://img.shields.io/github/license/NodeSecure/Cli?style=for-the-badge)](https://github.com/NodeSecure/cli/blob/master/workspaces/size-satisfies/LICENSE)
7+
![size](https://img.shields.io/github/languages/code-size/NodeSecure/size-satisfies?style=for-the-badge)
8+
[![build](https://img.shields.io/github/actions/workflow/status/NodeSecure/cli/nodejs.yml?style=for-the-badge)](https://github.com/NodeSecure/cli/actions?query=workflow%3A%22Node.js+CI%22)
9+
10+
Same as SemVer.satisfies but for file size!
11+
12+
## Requirements
13+
14+
- [Node.js](https://nodejs.org/en/) v18 or higher
15+
16+
## Getting Started
17+
18+
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).
19+
20+
```bash
21+
$ npm i @nodesecure/size-satisfies
22+
# or
23+
$ yarn add @nodesecure/size-satisfies
24+
```
25+
26+
## Usage example
27+
28+
```js
29+
import { strict } from "assert";
30+
import sizeSatisfies from "size-satisfies";
31+
32+
const { strictEqual } = strict;
33+
34+
strictEqual(sizeSatisfies(">= 45KB", "20MB"), true);
35+
strictEqual(sizeSatisfies("= 1MB", "1MB"), true);
36+
strictEqual(sizeSatisfies("= 1MB", 2000), false);
37+
```
38+
39+
The first argument of the `sizeSatisfies` method is the pattern with the operator + size. Available operators are `>=`, `<=`, `>`, `<`, `=`.
40+
41+
## API
42+
43+
### sizeSatisfies(pattern: string, size: number | string): boolean
44+
45+
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.
46+
47+
Invalid pattern will always return **false**.
48+
49+
## Contributors ✨
50+
51+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
52+
53+
[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)
54+
55+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
56+
57+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
58+
59+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
60+
<!-- prettier-ignore-start -->
61+
<!-- markdownlint-disable -->
62+
<table>
63+
<tbody>
64+
<tr>
65+
<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>
66+
<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>
67+
<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>
68+
<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>
69+
<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>
70+
<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>
71+
</tr>
72+
</tbody>
73+
</table>
74+
75+
<!-- markdownlint-restore -->
76+
<!-- prettier-ignore-end -->
77+
78+
<!-- ALL-CONTRIBUTORS-LIST:END -->
79+
80+
## License
81+
82+
MIT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare function sizeSatisfies(pattern: string, size: number | string): boolean;
2+
3+
export = sizeSatisfies;

workspaces/size-satisfies/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Require Third-party Dependencies
2+
import bytes from "bytes";
3+
4+
// CONSTANTS
5+
const kOperators = {
6+
">=": (lh, rh) => lh >= rh,
7+
"<=": (lh, rh) => lh <= rh,
8+
">": (lh, rh) => lh > rh,
9+
"<": (lh, rh) => lh < rh,
10+
"=": (lh, rh) => lh === rh
11+
};
12+
13+
/**
14+
* @function sizeSatisfies
15+
* @param {!string} pattern
16+
* @param {!(number | string)} size
17+
* @returns {boolean}
18+
*/
19+
function sizeSatisfies(pattern, size) {
20+
const localSize = typeof size === "number" ? size : bytes(size);
21+
const regexResult = /^(?<operator>[><=]{1,2})\s*(?<patternSize>.*)/g.exec(pattern);
22+
if (regexResult === null) {
23+
return false;
24+
}
25+
const { operator, patternSize } = regexResult.groups;
26+
27+
return kOperators[operator](localSize, bytes(patternSize));
28+
}
29+
30+
export default sizeSatisfies;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@nodesecure/size-satisfies",
3+
"version": "1.1.0",
4+
"description": "Same as SemVer.satisfies but for file size!",
5+
"exports": "./index.js",
6+
"scripts": {
7+
"lint": "eslint index.js",
8+
"test-only": "node --test test/",
9+
"test": "npm run lint && npm run test-only"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/NodeSecure/size-satisfies.git"
14+
},
15+
"files": [
16+
"index.js",
17+
"index.d.ts"
18+
],
19+
"keywords": [
20+
"size",
21+
"satisfies",
22+
"utility",
23+
"parse",
24+
"parser",
25+
"convert",
26+
"converter"
27+
],
28+
"engines": {
29+
"node": ">=18.0.0"
30+
},
31+
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",
32+
"license": "MIT",
33+
"bugs": {
34+
"url": "https://github.com/NodeSecure/size-satisfies/issues"
35+
},
36+
"homepage": "https://github.com/NodeSecure/size-satisfies#readme",
37+
"dependencies": {
38+
"bytes": "^3.1.2"
39+
},
40+
"devDependencies": {
41+
"@nodesecure/eslint-config": "^1.6.0"
42+
},
43+
"type": "module"
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert";
3+
4+
5+
// Require Internal Dependencies
6+
import sizeSatisfies from "../index.js";
7+
8+
test("invalid operator always return false", () => {
9+
assert.strictEqual(sizeSatisfies("!! 45KB", "45KB"), false);
10+
});
11+
12+
test("assert sizeSatisfies", () => {
13+
assert.strictEqual(sizeSatisfies(">= 45KB", "20MB"), true);
14+
assert.strictEqual(sizeSatisfies("<= 45KB", "10B"), true);
15+
assert.strictEqual(sizeSatisfies("= 45KB", "45KB"), true);
16+
assert.strictEqual(sizeSatisfies("= 45KB", "46KB"), false);
17+
assert.strictEqual(sizeSatisfies("= 45KB", 2000), false);
18+
assert.strictEqual(sizeSatisfies("> 45KB", "46KB"), true);
19+
assert.strictEqual(sizeSatisfies("> 45KB", "45KB"), false);
20+
assert.strictEqual(sizeSatisfies("< 45KB", "44KB"), true);
21+
});
22+
23+

0 commit comments

Comments
 (0)