First Relase
This commit is contained in:
parent
f2cf7e662c
commit
ee2dd27597
4 changed files with 75 additions and 4 deletions
26
README.md
26
README.md
|
@ -1 +1,27 @@
|
||||||
# template-npm-packages
|
# 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
|
||||||
|
```
|
||||||
|
|
42
index.ts
42
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
|
* Check if a permission is granted
|
||||||
* @param {Array<String>} perm - Is the permission that the user have
|
* @param {Array<String>} perm - Is the permission that the user have
|
||||||
|
@ -5,15 +38,20 @@
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
function checkList(perm: string[], reqPerm: string[]) {
|
function checkList(perm: string[], reqPerm: string[]) {
|
||||||
let answer = false, trueL = 0;
|
let answer = false, tl = 0, rql = reqPerm.length;
|
||||||
|
|
||||||
|
for (const rp of reqPerm) {
|
||||||
for (const p of perm) {
|
for (const p of perm) {
|
||||||
|
if (checkSingle(p, rp)) tl++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rql == tl) answer = true
|
||||||
|
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
checkList,
|
checkList,
|
||||||
|
checkSingle
|
||||||
}
|
}
|
|
@ -3,11 +3,15 @@
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "A permission checker for Node JS",
|
"description": "A permission checker for Node JS",
|
||||||
"main": "./build/index.js",
|
"main": "./build/index.js",
|
||||||
|
"type": "commonjs",
|
||||||
"repository": "https://github.com/ThunderNetworkRaD/template-npm-packages",
|
"repository": "https://github.com/ThunderNetworkRaD/template-npm-packages",
|
||||||
"author": "ThunderNetworkRaD | Killer Boss Original",
|
"author": "ThunderNetworkRaD | Killer Boss Original",
|
||||||
"license": "Mozilla Public License 2.0",
|
"license": "Mozilla Public License 2.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"typescript": "^5.1.3"
|
"typescript": "^5.1.3"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "tsc && node ."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
||||||
|
|
||||||
/* Modules */
|
/* 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. */,
|
"rootDir": "" /* Specify the root folder within your source files. */,
|
||||||
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
"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. */
|
// "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. */
|
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||||
},
|
},
|
||||||
|
"ts-node": {
|
||||||
|
"esm": true // «———— enabling ESM for ts-node
|
||||||
|
},
|
||||||
// "include": [],
|
// "include": [],
|
||||||
"exclude": ["node_modules", "logs", "build", "d", "test.js", "old", "tsi-*"]
|
"exclude": ["node_modules", "logs", "build", "d", "test.js", "old", "tsi-*"]
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue