ciam-commons
Advanced tools
Comparing version 1.14.1 to 1.14.2
236
index.js
@@ -1,144 +0,118 @@ | ||
"use strict"; | ||
var Utils; | ||
(function (Utils) { | ||
function unique(arr) { | ||
return arr.filter((v, i, a) => a.indexOf(v) === i); | ||
export class PermissionError extends Error { | ||
missing; | ||
constructor(missing) { | ||
super(`Missing permissions: ${(missing || []).join(', ')}`); | ||
this.name = 'PermissionError'; | ||
this.missing = missing || []; | ||
} | ||
Utils.unique = unique; | ||
function difference(a, b) { | ||
const arr = new Array(); | ||
for (const e of a) { | ||
if (!b.includes(e)) { | ||
arr.push(e); | ||
} | ||
} | ||
return arr; | ||
} | ||
/** | ||
* Check if a string is a valid permission flag. | ||
* | ||
* @param perm a string to check. | ||
* @returns true if {@link perm} is a valid permission flag. | ||
*/ | ||
export function validFlag(perm) { | ||
if (perm.length == 0) | ||
return false; | ||
try { | ||
flag(perm); | ||
return true; | ||
} | ||
Utils.difference = difference; | ||
})(Utils || (Utils = {})); | ||
var Perms; | ||
(function (Perms) { | ||
class PermissionError extends Error { | ||
missing; | ||
constructor(missing) { | ||
super(`Missing permissions: ${(missing || []).join(', ')}`); | ||
this.name = 'PermissionError'; | ||
this.missing = missing || []; | ||
} | ||
catch (err) { | ||
return false; | ||
} | ||
Perms.PermissionError = PermissionError; | ||
/** | ||
* Check if a string is a valid permission flag. | ||
* | ||
* @param perm a string to check. | ||
* @returns true if {@link perm} is a valid permission flag. | ||
*/ | ||
function validFlag(perm) { | ||
if (perm.length == 0) | ||
return false; | ||
try { | ||
Checks.flag(perm); | ||
return true; | ||
} | ||
catch (err) { | ||
return false; | ||
} | ||
} | ||
// Our lord an savior Ash has come to bless us | ||
export class Flag extends String { | ||
isWildcard; | ||
keys; | ||
constructor(value) { | ||
flag(value); | ||
super(value); | ||
this.isWildcard = value == '*' || value.endsWith('.*'); | ||
this.keys = value.split('.'); | ||
} | ||
Perms.validFlag = validFlag; | ||
// Our lord an savior Ash has come to bless us | ||
class Flag extends String { | ||
isWildcard; | ||
keys; | ||
constructor(value) { | ||
Checks.flag(value); | ||
super(value); | ||
this.isWildcard = value == '*' || value.endsWith('.*'); | ||
this.keys = value.split('.'); | ||
static validate(value) { | ||
if (!(value instanceof Flag)) { | ||
return new Flag(value); | ||
} | ||
static validate(value) { | ||
if (!(value instanceof Flag)) { | ||
return new Flag(value); | ||
} | ||
return value; | ||
} | ||
equals(other) { | ||
return this.toString() == other.toString(); | ||
} | ||
return value; | ||
} | ||
Perms.Flag = Flag; | ||
function flagArray(perms, ignoreInvalid = false, removeDuplicate = true) { | ||
const valid = new Array(); | ||
for (const p of perms) { | ||
if (ignoreInvalid) { | ||
try { | ||
valid.push(Flag.validate(p)); | ||
} | ||
catch (e) { } | ||
} | ||
else { | ||
equals(other) { | ||
return this.toString() == other.toString(); | ||
} | ||
} | ||
export function flagArray(perms, ignoreInvalid = false, removeDuplicate = true) { | ||
const valid = new Array(); | ||
for (const p of perms) { | ||
if (ignoreInvalid) { | ||
try { | ||
valid.push(Flag.validate(p)); | ||
} | ||
catch (e) { } | ||
} | ||
return removeDuplicate ? Utils.unique(valid) : valid; | ||
else { | ||
valid.push(Flag.validate(p)); | ||
} | ||
} | ||
Perms.flagArray = flagArray; | ||
})(Perms || (Perms = {})); | ||
var Checks; | ||
(function (Checks) { | ||
Checks.objectIdRegex = /[a-f0-9]{24}/; | ||
Checks.discordIdRegex = /\d{16,20}/; | ||
Checks.flagRegex = /^(?:([a-z0-9]+|\?)(?:\.(?:[a-z0-9]+|\?))*(\.\*)?|\*)$/; | ||
Checks.strictFlagRegex = /^[a-z0-9]+(\.[a-z0-9]+)*$/; | ||
class CheckError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'CheckError'; | ||
return removeDuplicate ? unique(valid) : valid; | ||
} | ||
export function unique(arr) { | ||
return arr.filter((v, i, a) => a.indexOf(v) === i); | ||
} | ||
export function difference(a, b) { | ||
const arr = new Array(); | ||
for (const e of a) { | ||
if (!b.includes(e)) { | ||
arr.push(e); | ||
} | ||
} | ||
Checks.CheckError = CheckError; | ||
function objectId(id, message = 'Invalid objectId') { | ||
if (!id.match(Checks.objectIdRegex)) | ||
throw new CheckError(`${message} "${id}"`); | ||
return arr; | ||
} | ||
export const objectIdRegex = /[a-f0-9]{24}/; | ||
export const discordIdRegex = /\d{16,20}/; | ||
export const flagRegex = /^(?:([a-z0-9]+|\?)(?:\.(?:[a-z0-9]+|\?))*(\.\*)?|\*)$/; | ||
export const strictFlagRegex = /^[a-z0-9]+(\.[a-z0-9]+)*$/; | ||
export class CheckError extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'CheckError'; | ||
} | ||
Checks.objectId = objectId; | ||
function discordId(id, message = 'Invalid discordId') { | ||
if (!id.match(Checks.discordIdRegex)) | ||
throw new CheckError(`${message} "${id}"`); | ||
} | ||
Checks.discordId = discordId; | ||
function flag(flag, message = 'Invalid permission flag') { | ||
if (!flag.match(Checks.flagRegex)) | ||
throw new CheckError(`${message} "${flag}"`); | ||
} | ||
Checks.flag = flag; | ||
function strictFlag(flag, message = 'Invalid strict permission flag') { | ||
if (!flag.match(Checks.strictFlagRegex)) | ||
throw new CheckError(`${message} "${flag}"`); | ||
} | ||
Checks.strictFlag = strictFlag; | ||
function notEmpty(obj, name) { | ||
if (obj.length == 0) | ||
throw new CheckError(`${name} cannot be empty`); | ||
} | ||
Checks.notEmpty = notEmpty; | ||
function min(n, min, name) { | ||
if (n < min) | ||
throw new CheckError(`${name} cannot be less than ${min}`); | ||
} | ||
Checks.min = min; | ||
function max(n, max, name) { | ||
if (n > max) | ||
throw new CheckError(`${name} cannot be greater than ${max}`); | ||
} | ||
Checks.max = max; | ||
function inRange(n, minVal, maxVal, name) { | ||
min(n, minVal, name); | ||
max(n, maxVal, name); | ||
} | ||
Checks.inRange = inRange; | ||
function oneOf(obj, options, name) { | ||
if (!options.includes(obj)) | ||
throw new CheckError(`${name} must be one of ${options.toString()}`); | ||
} | ||
Checks.oneOf = oneOf; | ||
})(Checks || (Checks = {})); | ||
} | ||
export function objectId(id, message = 'Invalid objectId') { | ||
if (!id.match(objectIdRegex)) | ||
throw new CheckError(`${message} "${id}"`); | ||
} | ||
export function discordId(id, message = 'Invalid discordId') { | ||
if (!id.match(discordIdRegex)) | ||
throw new CheckError(`${message} "${id}"`); | ||
} | ||
export function flag(flag, message = 'Invalid permission flag') { | ||
if (!flag.match(flagRegex)) | ||
throw new CheckError(`${message} "${flag}"`); | ||
} | ||
export function strictFlag(flag, message = 'Invalid strict permission flag') { | ||
if (!flag.match(strictFlagRegex)) | ||
throw new CheckError(`${message} "${flag}"`); | ||
} | ||
export function notEmpty(obj, name) { | ||
if (obj.length == 0) | ||
throw new CheckError(`${name} cannot be empty`); | ||
} | ||
export function min(n, min, name) { | ||
if (n < min) | ||
throw new CheckError(`${name} cannot be less than ${min}`); | ||
} | ||
export function max(n, max, name) { | ||
if (n > max) | ||
throw new CheckError(`${name} cannot be greater than ${max}`); | ||
} | ||
export function inRange(n, minVal, maxVal, name) { | ||
min(n, minVal, name); | ||
max(n, maxVal, name); | ||
} | ||
export function oneOf(obj, options, name) { | ||
if (!options.includes(obj)) | ||
throw new CheckError(`${name} must be one of ${options.toString()}`); | ||
} |
{ | ||
"name": "ciam-commons", | ||
"version": "1.14.1", | ||
"version": "1.14.2", | ||
"description": "Common types and functions for CIAM", | ||
@@ -14,6 +14,10 @@ "scripts": { | ||
"type": "module", | ||
"exports": [ | ||
"./index.js", | ||
"./index.d.ts" | ||
], | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"dist" | ||
] | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3909
2
118