From 0d7df525b613d5decbbfc05fb069217ba15f6b7c Mon Sep 17 00:00:00 2001 From: Sam Donald <32623552+mudlabs@users.noreply.github.com> Date: Sun, 11 Oct 2020 12:45:38 +1100 Subject: [PATCH 1/6] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9897ef2..57c0ae7 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ jobs: | Prop | Description | Default | | :--- | :--- | :--- | | `clean` | Specifies the action should edit the configuration file apon execution. This way every time you update the file it's a clean list of commands. Only the `install`, `update`, and `uninstall` arrays will be emptied, and they will be regardless of the package execution outcome. | `false` | +| `init` | Specifies the action should create a `package.json` file at `path` if one is not there. If the package does need to init, it will do so using `npm init -y`. | `true` | | `issue` | You may provide an issue number to track activity. If set this action will post comments to the issue detailing what has changed, whenever it runs. For an example of what these comments will look like see the [Test Logs](https://github.com/mudlabs/npm-worker/issues/4) issue. | | | `path` | Specifies a path from your repository _root_, where you would like _node_modules_ located. | `./` | | `install` | An array of npm packages you want installed. | | @@ -81,6 +82,7 @@ jobs: ```yaml # Example Configuration clean: true +init: true issue: 1 path: ./dis install: @@ -104,7 +106,6 @@ uninstall: ## Notes -- If no `package.json` file is located at `path`, the action will create one using `npm init -y`. - If you want the `node_modules` and `packages` to persist on your repository you will need to commit the changes. I recommend using the [Add and Commit](https://github.com/marketplace/actions/add-commit) action for this. - You can not chain executions. Each array item is checked for, and broken at any instance of `&&`. - Each array item is executed as `npm x item` _(i.e. npm install unirest)_. From 2e53d27f508411a8f8c7b7d10237297a4f0c8013 Mon Sep 17 00:00:00 2001 From: Sam Donald <32623552+mudlabs@users.noreply.github.com> Date: Sun, 11 Oct 2020 13:23:58 +1100 Subject: [PATCH 2/6] Update action.js --- action.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/action.js b/action.js index 43d65b6..8f8ccfe 100644 --- a/action.js +++ b/action.js @@ -85,8 +85,9 @@ const getWorkerConfigPath = workflow => { return found_config_path; }; -const initJSON = async path => { +const initJSON = path => async init => { try { + if (!init) return; const file_path = `${path}/package.json`; const {stdout} = await execa.command('npm init -y'); const json = stdout.substring(stdout.indexOf("{")); @@ -118,7 +119,7 @@ const initJSON = async path => { const has_package_json = fs.existsSync(`${node_modules_path}/package.json`); if (!valid_node_modules_path) return core.setFailed(`The path for node_modules does not exist.`); - if (!has_package_json) await initJSON(node_modules_path); + if (!has_package_json) await initJSON(node_modules_path)(data.init); const installed = await shell("install")(data.install)(node_modules_path); const updated = await shell("update")(data.update)(node_modules_path); From b7d75b4a309c54b717d5f0ed0a44e850f2c90f9a Mon Sep 17 00:00:00 2001 From: Sam Donald <32623552+mudlabs@users.noreply.github.com> Date: Sun, 11 Oct 2020 13:29:39 +1100 Subject: [PATCH 3/6] Delete package.json --- test/package.json | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 test/package.json diff --git a/test/package.json b/test/package.json deleted file mode 100644 index 89dfacb..0000000 --- a/test/package.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "npm-worker", - "version": "1.0.0", - "description": "[action-badge]: https://img.shields.io/badge/-Action-24292e?logo=github&style=for-the-badge [paypal-badge]: https://img.shields.io/badge/-Support-f3f4f6?logo=paypal&style=for-the-badge [brave-badge]: https://img.shields.io/badge/-Tip-f3f4f6?logo=brave&style=for-the-badge", - "main": "action.js", - "dependencies": { - "@actions/core": "^1.2.6", - "@actions/github": "^4.0.0", - "cardinal-direction": "^1.0.0", - "execa": "^4.0.3" - }, - "devDependencies": {}, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/mudlabs/npm-worker.git" - }, - "keywords": [], - "author": "", - "license": "ISC", - "bugs": { - "url": "https://github.com/mudlabs/npm-worker/issues" - }, - "homepage": "https://github.com/mudlabs/npm-worker#readme", - "directories": { - "test": "test" - } -} From 22e828fb0809b1b40178aff5d33cc6b8aa17a8e5 Mon Sep 17 00:00:00 2001 From: Sam Donald <32623552+mudlabs@users.noreply.github.com> Date: Sun, 11 Oct 2020 13:37:11 +1100 Subject: [PATCH 4/6] Update action.js --- action.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/action.js b/action.js index 8f8ccfe..91af4bb 100644 --- a/action.js +++ b/action.js @@ -87,8 +87,11 @@ const getWorkerConfigPath = workflow => { const initJSON = path => async init => { try { - if (!init) return; const file_path = `${path}/package.json`; + + if (init === false) return; + if (fs.existsSync(file_path)) return; + const {stdout} = await execa.command('npm init -y'); const json = stdout.substring(stdout.indexOf("{")); await fs.promises.writeFile(file_path, json); From 8b7a5296fd10a3bc89e57c69b6a599d3b18b22a4 Mon Sep 17 00:00:00 2001 From: Sam Donald <32623552+mudlabs@users.noreply.github.com> Date: Sun, 11 Oct 2020 13:39:14 +1100 Subject: [PATCH 5/6] Update npm.worker.config.yaml --- test/npm.worker.config.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/npm.worker.config.yaml b/test/npm.worker.config.yaml index f02c57d..1823321 100644 --- a/test/npm.worker.config.yaml +++ b/test/npm.worker.config.yaml @@ -1,6 +1,7 @@ clean: true issue: 4 +init: false path: ./test/ -install: [] -update: [] +install: [ short-scale-units ] +update: [ execa ] uninstall: [] From 74d01a5427e081badba32393b7b02380e6d82f8c Mon Sep 17 00:00:00 2001 From: Sam Donald <32623552+mudlabs@users.noreply.github.com> Date: Sun, 11 Oct 2020 02:39:44 +0000 Subject: [PATCH 6/6] Commit from GitHub Actions (Test Worker) --- test/node_modules/short-scale-units/README.md | 121 ++++++++++++++++++ .../short-scale-units/dist/index.d.ts | 40 ++++++ .../short-scale-units/dist/index.js | 111 ++++++++++++++++ .../short-scale-units/package.json | 85 ++++++++++++ test/npm.worker.config.yaml | 4 +- test/package-lock.json | 9 +- 6 files changed, 365 insertions(+), 5 deletions(-) create mode 100644 test/node_modules/short-scale-units/README.md create mode 100644 test/node_modules/short-scale-units/dist/index.d.ts create mode 100644 test/node_modules/short-scale-units/dist/index.js create mode 100644 test/node_modules/short-scale-units/package.json diff --git a/test/node_modules/short-scale-units/README.md b/test/node_modules/short-scale-units/README.md new file mode 100644 index 0000000..4eaef2c --- /dev/null +++ b/test/node_modules/short-scale-units/README.md @@ -0,0 +1,121 @@ +[npm]: https://img.shields.io/npm/v/short-scale-units.svg?color=949393 +[install size]: https://badgen.net/packagephobia/install/short-scale-units?color=949393 +[support]: https://img.shields.io/static/v1.svg?logo=paypal&label=Support&message=Mudlabs&style=for-the-badge&color=0c67b5&labelColor=afb0b9 + +[![npm]](https://www.npmjs.com/package/short-scale-units) +[![install size]](https://www.npmjs.com/package/short-scale-units) +
+[![support]](https://paypal.me/mudlabs) + +# Short Scale Units + +A simple package providing helper functions for matching numbers with their _short scale unit_. + +### Table of Context + - [Installation](#Installation) + - [Import/Require](#Import/Require) + - [API](#API) + - [unitNameFromNumber](#unitNameFromNumber) + - [numberFromUnitName](#numberFromUnitName) + - [trimNumber](#trimNumber) + - [trimName](#trimName) + - [getUnitPower](#getUnitPower) + - [getPower](#getPower) + - [Supported Units](#Supported-Units) + +### Installation +``` +npm install short-scale-units +``` + +--- + +### Import/Require +```js +// TypeScript +import * as ssu from "short-scale-units"; + +// JavaScript +const ssu = require("short-scale-units"); +``` + +--- + +### API + +#### unitNameFromNumber +Provides the English unit name for the given number. +```js +const name = ssu.unitNameFromNumber(17388); +// name = "thousand" +``` + +#### numberFromUnitName +Provides the base unit number for the given unit name. +```js +const number = ssu.numberFromUnitName("thousand"); +// number = 1000 +``` + +#### trimNumber +Provides the short-hand abbreviation of a number, relative to its unit. +```js +const trimmed = ssu.trimNumber(17388); +// trimmed = 17 (because 17388 is 17 thousands) +``` + +#### trimName +Provides an abbreviation of the unit name; +```js +const abbreviation = trimName("thousand"); +// abbreviation = "K" +``` + +#### getUnitPower +Provides the power for the given number, relative to its unit. +- If you want the numbers real power use `getPower`. +```js +const unitPower = ssu.getUnitPower(17388); +// unitPower = 3 +``` + +#### getPower +Provides the power for the given number. +- If you want the power relative the its unit use `getUnitPower`. +```js +const powerRound = ssu.getPower(17388, true); +const power = ssu.getPower(17388); +// powerRound = 4 +// power = 4.240249631518799 +``` + +--- + +### Supported Units + +| Unit Name | Unit Power | Unit Abbreviation | +| --------- | ---------- | ----------------- | +| one | 0 | O | +| ten | 1 | T | +| hundred | 2 | H | +| thousand | 3 | K | +| million | 6 | M | +| billion | 9 | B | +| trillion | 12 | Tr | +| quadrillion | 15 | Qd | +| quintillion | 18 | Qt | +| sextillion | 21 | Sxt | +| septillion | 24 | Spt | +| octillion | 27 | Ot | +| nonillion | 30 | Nn | +| decillion | 33 | Dc | +| undecillion | 36 | Ud | +| duodecillion | 39 | Dd | +| tredecillion | 42 | Td | +| quattuordecillion | 45 | Qtd | +| quindecillion | 48 | Qnd | +| sexdecillion | 51 | Sxd | +| septendecillion | 54 | Std | +| octodecillion | 57 | Od | +| novemdecillion | 60 | Nd | +| vigintillion | 63 | V | \ No newline at end of file diff --git a/test/node_modules/short-scale-units/dist/index.d.ts b/test/node_modules/short-scale-units/dist/index.d.ts new file mode 100644 index 0000000..892261c --- /dev/null +++ b/test/node_modules/short-scale-units/dist/index.d.ts @@ -0,0 +1,40 @@ +/** + * Provides the English unit name for the given number. + * @param number The number you want to match a unit to. + * @returns The matched unit name. + */ +export declare function unitNameFromNumber(number: number): string; +/** + * Provides the base unit number for the given unit name. + * @param name The unit name you want the base number of. + * @param raw Specify `true` if you want the **raw unit integer**. Otherwise large numbers will be in scientific notation `1e+X`. + * @returns The unit value as a `number`, or a `string` if `raw` was specified true. + */ +export declare function numberFromUnitName(name: string, raw?: boolean): number | string; +/** + * Provides an abbreviation of the short unit name; + * @param name The full unit name. + * @returns The abbreviated unit name. + */ +export declare function trimName(name: string): string; +/** + * Provides the short-hand abbreviation of a number, relative to its unit. + * @param number The number you want abbreviated to its unit value. + * @returns The shortend number. + */ +export declare function trimNumber(number: number): number; +/** + * Provides the power for the given number, relative to its unit. + * - If you want the real power see `getPower`. + * @param number The number for which you want the unit power. + * @returns The numbers unit power. + */ +export declare function getUnitPower(number: number): number; +/** + * Provides the power for the given number. + * - If you want the power relative to its unit use `getUnitPower`. + * @param number The number for which you want the power. + * @param round A boolean specifing the power should be rounded. + * @returns The numbers power + */ +export declare function getPower(number: number, round?: boolean): number; diff --git a/test/node_modules/short-scale-units/dist/index.js b/test/node_modules/short-scale-units/dist/index.js new file mode 100644 index 0000000..e6c2b43 --- /dev/null +++ b/test/node_modules/short-scale-units/dist/index.js @@ -0,0 +1,111 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var Unit; +(function (Unit) { + Unit[Unit["one"] = 0] = "one"; + Unit[Unit["ten"] = 1] = "ten"; + Unit[Unit["hundred"] = 2] = "hundred"; + Unit[Unit["thousand"] = 3] = "thousand"; + Unit[Unit["million"] = 6] = "million"; + Unit[Unit["billion"] = 9] = "billion"; + Unit[Unit["trillion"] = 12] = "trillion"; + Unit[Unit["quadrillion"] = 15] = "quadrillion"; + Unit[Unit["quintillion"] = 18] = "quintillion"; + Unit[Unit["sextillion"] = 21] = "sextillion"; + Unit[Unit["septillion"] = 24] = "septillion"; + Unit[Unit["octillion"] = 27] = "octillion"; + Unit[Unit["nonillion"] = 30] = "nonillion"; + Unit[Unit["decillion"] = 33] = "decillion"; + Unit[Unit["undecillion"] = 36] = "undecillion"; + Unit[Unit["duodecillion"] = 39] = "duodecillion"; + Unit[Unit["tredecillion"] = 42] = "tredecillion"; + Unit[Unit["quattuordecillion"] = 45] = "quattuordecillion"; + Unit[Unit["quindecillion"] = 48] = "quindecillion"; + Unit[Unit["sexdecillion"] = 51] = "sexdecillion"; + Unit[Unit["septendecillion"] = 54] = "septendecillion"; + Unit[Unit["octodecillion"] = 57] = "octodecillion"; + Unit[Unit["novemdecillion"] = 60] = "novemdecillion"; + Unit[Unit["vigintillion"] = 63] = "vigintillion"; +})(Unit || (Unit = {})); +var UnitAbbreviation; +(function (UnitAbbreviation) { + UnitAbbreviation[UnitAbbreviation["O"] = 0] = "O"; + UnitAbbreviation[UnitAbbreviation["T"] = 1] = "T"; + UnitAbbreviation[UnitAbbreviation["H"] = 2] = "H"; + UnitAbbreviation[UnitAbbreviation["K"] = 3] = "K"; + UnitAbbreviation[UnitAbbreviation["M"] = 6] = "M"; + UnitAbbreviation[UnitAbbreviation["B"] = 9] = "B"; + UnitAbbreviation[UnitAbbreviation["Tr"] = 12] = "Tr"; + UnitAbbreviation[UnitAbbreviation["Qd"] = 15] = "Qd"; + UnitAbbreviation[UnitAbbreviation["Qt"] = 18] = "Qt"; + UnitAbbreviation[UnitAbbreviation["Sxt"] = 21] = "Sxt"; + UnitAbbreviation[UnitAbbreviation["Spt"] = 24] = "Spt"; + UnitAbbreviation[UnitAbbreviation["Ot"] = 27] = "Ot"; + UnitAbbreviation[UnitAbbreviation["Nn"] = 30] = "Nn"; + UnitAbbreviation[UnitAbbreviation["Dc"] = 33] = "Dc"; + UnitAbbreviation[UnitAbbreviation["Ud"] = 36] = "Ud"; + UnitAbbreviation[UnitAbbreviation["Dd"] = 39] = "Dd"; + UnitAbbreviation[UnitAbbreviation["Td"] = 42] = "Td"; + UnitAbbreviation[UnitAbbreviation["Qtd"] = 45] = "Qtd"; + UnitAbbreviation[UnitAbbreviation["Qnd"] = 48] = "Qnd"; + UnitAbbreviation[UnitAbbreviation["Sxd"] = 51] = "Sxd"; + UnitAbbreviation[UnitAbbreviation["Std"] = 54] = "Std"; + UnitAbbreviation[UnitAbbreviation["Od"] = 57] = "Od"; + UnitAbbreviation[UnitAbbreviation["Nd"] = 60] = "Nd"; + UnitAbbreviation[UnitAbbreviation["V"] = 63] = "V"; +})(UnitAbbreviation || (UnitAbbreviation = {})); +function unitNameFromNumber(number) { + const power = getUnitPower(number); + return Unit[power]; +} +exports.unitNameFromNumber = unitNameFromNumber; +function numberFromUnitName(name, raw) { + const power = Unit[name.toLowerCase()]; + const unitNumber = Math.pow(10, power); + const isScientific = new RegExp(/e\+\d+$/).test(unitNumber.toString()); + if (raw && isScientific) { + let string = "1"; + const power = parseInt(unitNumber.toString().split("+")[1]); + for (let i = 0; i < power; i++) { + string += "0"; + } + return string; + } + else if (raw) { + return unitNumber.toString(); + } + else { + return unitNumber; + } +} +exports.numberFromUnitName = numberFromUnitName; +function trimName(name) { + const power = Unit[name]; + return UnitAbbreviation[power]; +} +exports.trimName = trimName; +function trimNumber(number) { + const power = getPower(number); + const base = Math.round(Math.max(power, 1)); + const value = power > 3 + ? Math.floor(power / 3) * 3 + : Math.round(power / base) * base; + return Math.floor(number / eval(`1e+${value}`)); +} +exports.trimNumber = trimNumber; +function getUnitPower(number) { + const power = Math.fround(getPower(number)); + const base = Math.floor(Math.max(power, 1)); + const value = power > 3 + ? Math.floor(power / 3) * 3 + : Math.floor(power / base) * base; + return value; +} +exports.getUnitPower = getUnitPower; +function getPower(number, round) { + const target = Math.max(number, 1); + return round + ? Math.round(Math.log(target) / Math.log(10)) + : Math.log(target) / Math.log(10); +} +exports.getPower = getPower; diff --git a/test/node_modules/short-scale-units/package.json b/test/node_modules/short-scale-units/package.json new file mode 100644 index 0000000..e5659d4 --- /dev/null +++ b/test/node_modules/short-scale-units/package.json @@ -0,0 +1,85 @@ +{ + "_from": "short-scale-units", + "_id": "short-scale-units@1.0.2", + "_inBundle": false, + "_integrity": "sha512-sxbiYZqokySN2lGSLwx2qf25kqA+w7VSs6kZ74Umc8m9XQ/lBoEoyMyKwT3dchTNXGUI0geYDaz0jruz5xblgQ==", + "_location": "/short-scale-units", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "short-scale-units", + "name": "short-scale-units", + "escapedName": "short-scale-units", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/short-scale-units/-/short-scale-units-1.0.2.tgz", + "_shasum": "830b2311963781c58603bed96342ae75b8ae0975", + "_spec": "short-scale-units", + "_where": "/home/runner/work/npm-worker/npm-worker", + "author": { + "name": "Mudlabs", + "email": "npm@mudlabs.com.au" + }, + "bugs": { + "url": "https://github.com/mudlabs/short-scale-units/issues" + }, + "bundleDependencies": false, + "dependencies": {}, + "deprecated": false, + "description": "A simple package providing helper functions for matching numbers with their _short scale unit_. And formatting the numbers, or the unit values", + "devDependencies": { + "@types/node": "^13.11.0" + }, + "files": [ + "dist/" + ], + "homepage": "https://github.com/mudlabs/short-scale-units#readme", + "keywords": [ + "Numbers", + "Short", + "Scale", + "Units", + "Power", + "Thousand", + "Million", + "Billion", + "Trillion", + "Quadrillion", + "Quintillion", + "Sextillion", + "Septillion", + "Octillion", + "Nonillion", + "Decillion", + "Undecillion", + "Duodecillion", + "Tredecillion", + "Quattuordecillion", + "Quindecillion", + "Sexdecillion", + "Septendecillion", + "Octodecillion", + "Novemdecillion", + "Vigintillion" + ], + "license": "MIT", + "main": "./dist/index.js", + "name": "short-scale-units", + "repository": { + "type": "git", + "url": "git+https://github.com/mudlabs/short-scale-units.git" + }, + "scripts": { + "prepare": "tsc && ts-node cleaner", + "test": "ts-node tests/test" + }, + "types": "./dist/index.d.ts", + "version": "1.0.2" +} diff --git a/test/npm.worker.config.yaml b/test/npm.worker.config.yaml index 1823321..14d3d5f 100644 --- a/test/npm.worker.config.yaml +++ b/test/npm.worker.config.yaml @@ -2,6 +2,6 @@ clean: true issue: 4 init: false path: ./test/ -install: [ short-scale-units ] -update: [ execa ] +install: [] +update: [] uninstall: [] diff --git a/test/package-lock.json b/test/package-lock.json index 8b27f5d..19e3f66 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -1,8 +1,6 @@ { - "name": "npm-worker", - "version": "1.0.0", - "lockfileVersion": 1, "requires": true, + "lockfileVersion": 1, "dependencies": { "@actions/core": { "version": "1.2.6", @@ -269,6 +267,11 @@ "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==" }, + "short-scale-units": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/short-scale-units/-/short-scale-units-1.0.2.tgz", + "integrity": "sha512-sxbiYZqokySN2lGSLwx2qf25kqA+w7VSs6kZ74Umc8m9XQ/lBoEoyMyKwT3dchTNXGUI0geYDaz0jruz5xblgQ==" + }, "signal-exit": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz",