Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration from size-satisfies to workspaces #302

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/size-satisfies.yml
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"workspaces": [
"workspaces/documentation-ui",
"workspaces/vis-network"
"workspaces/vis-network",
"workspaces/size-satisfies"
],
"repository": {
"type": "git",
Expand Down
21 changes: 21 additions & 0 deletions workspaces/size-satisfies/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
51 changes: 51 additions & 0 deletions workspaces/size-satisfies/README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions workspaces/size-satisfies/index.d.ts
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;
30 changes: 30 additions & 0 deletions workspaces/size-satisfies/index.js
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;
41 changes: 41 additions & 0 deletions workspaces/size-satisfies/package.json
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!",
"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 <gentilhomme.thomas@gmail.com>",
"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"
}
}
23 changes: 23 additions & 0 deletions workspaces/size-satisfies/test/test.js
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);
});


Loading