From ee2dd275978ee33ef0d3c36c7ebc2460deb6d59f Mon Sep 17 00:00:00 2001 From: KillerBossOriginal Date: Wed, 7 Jun 2023 21:38:04 +0200 Subject: [PATCH] First Relase --- README.md | 26 ++++++++++++++++++++++++++ index.ts | 44 +++++++++++++++++++++++++++++++++++++++++--- package.json | 4 ++++ tsconfig.json | 5 ++++- 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3039f56..d1b40c0 100644 --- a/README.md +++ b/README.md @@ -1 +1,27 @@ # template-npm-packages + +/!\ We haven't tested it on pure JavaScript and on CommonJS yet. + +## checklist + +```ts +import { checkList } from "permission-checker"; + +console.log(checkList(["permission1", "permission2.subPermission1"], ["requiredPermission1"])) +// permission1 != requiredPermission1 && permission2.subPermission1 != requiredPermission1, checkList = false +console.log(checkList(["*"], ["requiredPermission1"])) +// * catch all, always true +console.log(checkList(["permission1"], ["*"])) +// if in the first array there isn't * this is always false + +console.log(checkList(["permission1"], ["permission1.subPermission1"])) +// permission1 includes subPermission1, this is true +``` + +## checksingle + +is the same of checklist but without an array. +```ts +import { checkSingle } from "permission-checker"; +console.log(checkSingle("*", "permission1")) // true +``` diff --git a/index.ts b/index.ts index 5466834..01fecf0 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,36 @@ +/** + * Check if a permission is granted + * @param {String} perm - Is the permission that the user have + * @param {String} reqPerm - Is the required permissions + * @returns {Boolean} + */ +function checkSingle(perm: string, reqPerm: string) { + let answer = false + + if (perm === "*") answer = true; + else { + let subRP = reqPerm.split('.'); + let sub = perm.split(".") + + if (subRP.length < sub.length) answer = false; + else { + let i = 0; + for (const rp of subRP) { + i = subRP.findIndex((e) => e === rp) + console.log(i, rp, sub[i], subRP.length) + if (rp != sub[i]) break; + else i++; + } + + if (i == subRP.length || i == sub.length) answer = true; + } + } + + + return answer; +} + + /** * Check if a permission is granted * @param {Array} perm - Is the permission that the user have @@ -5,15 +38,20 @@ * @returns {Boolean} */ function checkList(perm: string[], reqPerm: string[]) { - let answer = false, trueL = 0; + let answer = false, tl = 0, rql = reqPerm.length; - for (const p of perm) { - + for (const rp of reqPerm) { + for (const p of perm) { + if (checkSingle(p, rp)) tl++; + } } + if (rql == tl) answer = true + return answer; } export default { checkList, + checkSingle } \ No newline at end of file diff --git a/package.json b/package.json index 6d39c37..ffea44f 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,15 @@ "version": "0.0.1", "description": "A permission checker for Node JS", "main": "./build/index.js", + "type": "commonjs", "repository": "https://github.com/ThunderNetworkRaD/template-npm-packages", "author": "ThunderNetworkRaD | Killer Boss Original", "license": "Mozilla Public License 2.0", "private": false, "dependencies": { "typescript": "^5.1.3" + }, + "scripts": { + "test": "tsc && node ." } } diff --git a/tsconfig.json b/tsconfig.json index 924e562..f6b1e28 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,7 +25,7 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "ESNext" /* Specify what module code is generated. */, + "module": "NodeNext" /* Specify what module code is generated. */, "rootDir": "" /* Specify the root folder within your source files. */, "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ @@ -100,6 +100,9 @@ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, + "ts-node": { + "esm": true // «———— enabling ESM for ts-node + }, // "include": [], "exclude": ["node_modules", "logs", "build", "d", "test.js", "old", "tsi-*"] }