From 692566b1fe823b7bc17129aa6d13bec0eb117f56 Mon Sep 17 00:00:00 2001 From: Kouadio Fabrice N'guessan Date: Mon, 4 Dec 2023 08:13:45 +0000 Subject: [PATCH] chore:Migration from size-satisfies to workspaces --- .github/workflows/size-satisfies.yml | 37 +++++++++++++++++++ README.md | 1 + package.json | 3 +- workspaces/size-satisfies/LICENSE | 21 +++++++++++ workspaces/size-satisfies/README.md | 51 ++++++++++++++++++++++++++ workspaces/size-satisfies/index.d.ts | 3 ++ workspaces/size-satisfies/index.js | 30 +++++++++++++++ workspaces/size-satisfies/package.json | 41 +++++++++++++++++++++ workspaces/size-satisfies/test/test.js | 23 ++++++++++++ 9 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/size-satisfies.yml create mode 100644 workspaces/size-satisfies/LICENSE create mode 100644 workspaces/size-satisfies/README.md create mode 100644 workspaces/size-satisfies/index.d.ts create mode 100644 workspaces/size-satisfies/index.js create mode 100644 workspaces/size-satisfies/package.json create mode 100644 workspaces/size-satisfies/test/test.js diff --git a/.github/workflows/size-satisfies.yml b/.github/workflows/size-satisfies.yml new file mode 100644 index 00000000..995eee66 --- /dev/null +++ b/.github/workflows/size-satisfies.yml @@ -0,0 +1,37 @@ +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 + \ No newline at end of file diff --git a/README.md b/README.md index 64294381..cfbb9173 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,7 @@ Click on one of the links to access the documentation of the workspace: | --- | --- | | documentation-ui | [@nodesecure/documentation-ui](./workspaces/documentation-ui) | | vis-network | [@nodesecure/vis-network ](./workspaces/vis-network) | +| size-satisfies | [@nodesecure/size-satisfies ](./workspaces/size-satisfies) | These packages are 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 diff --git a/package.json b/package.json index ad908491..c8ba16c9 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ ], "workspaces": [ "workspaces/documentation-ui", - "workspaces/vis-network" + "workspaces/vis-network", + "workspaces/size-satisfies" ], "repository": { "type": "git", diff --git a/workspaces/size-satisfies/LICENSE b/workspaces/size-satisfies/LICENSE new file mode 100644 index 00000000..346097d0 --- /dev/null +++ b/workspaces/size-satisfies/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 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. diff --git a/workspaces/size-satisfies/README.md b/workspaces/size-satisfies/README.md new file mode 100644 index 00000000..17d05938 --- /dev/null +++ b/workspaces/size-satisfies/README.md @@ -0,0 +1,51 @@ +# `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**. + +## License + +MIT diff --git a/workspaces/size-satisfies/index.d.ts b/workspaces/size-satisfies/index.d.ts new file mode 100644 index 00000000..d4512cf8 --- /dev/null +++ b/workspaces/size-satisfies/index.d.ts @@ -0,0 +1,3 @@ +declare function sizeSatisfies(pattern: string, size: number | string): boolean; + +export = sizeSatisfies; diff --git a/workspaces/size-satisfies/index.js b/workspaces/size-satisfies/index.js new file mode 100644 index 00000000..d0ee58e9 --- /dev/null +++ b/workspaces/size-satisfies/index.js @@ -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 = /^(?[><=]{1,2})\s*(?.*)/g.exec(pattern); + if (regexResult === null) { + return false; + } + const { operator, patternSize } = regexResult.groups; + + return kOperators[operator](localSize, bytes(patternSize)); +} + +export default sizeSatisfies; diff --git a/workspaces/size-satisfies/package.json b/workspaces/size-satisfies/package.json new file mode 100644 index 00000000..b385207b --- /dev/null +++ b/workspaces/size-satisfies/package.json @@ -0,0 +1,41 @@ +{ + "name": "@nodesecure/size-satisfies", + "version": "1.1.0", + "description": "Same as SemVer.satisfies but for file size!", + "type": "module", + "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/cli.git" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "size", + "satisfies", + "utility", + "parse", + "parser", + "convert", + "converter" + ], + "engines": { + "node": ">=18.0.0" + }, + "author": "GENTILHOMME Thomas ", + "license": "MIT", + "bugs": { + "url": "https://github.com/NodeSecure/cli/issues" + }, + "homepage": "https://github.com/NodeSecure/cli/master/workspaces/size-satisfies#readme", + "dependencies": { + "bytes": "^3.1.2" + } +} diff --git a/workspaces/size-satisfies/test/test.js b/workspaces/size-satisfies/test/test.js new file mode 100644 index 00000000..a3360cb5 --- /dev/null +++ b/workspaces/size-satisfies/test/test.js @@ -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); +}); + +