@litert/core
Advanced tools
Comparing version 1.1.0 to 1.1.1
# Changes Logs | ||
## v1.1.1 | ||
- Added `ErrorHub.codeIndex`. | ||
- Fixed the return type of `ErrorHub.define`. | ||
## v1.1.0 | ||
@@ -4,0 +9,0 @@ |
@@ -18,13 +18,13 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
const errors = Core.getDefaultErrorHub(); | ||
const TEST_ERROR = errors.define(123, "TEST_ERROR", "This is a test error.", {}); | ||
const TEST_ERROR = errors.define(123, 'TEST_ERROR', 'This is a test error.', {}); | ||
console.error(JSON.stringify(new TEST_ERROR().toJSON(true), null, 2)); | ||
const myErrors = Core.createErrorHub("@litert/core#error-example"); | ||
const myErrors = Core.createErrorHub('@litert/core#error-example'); | ||
/** | ||
* Type 1 of error. | ||
*/ | ||
const MY_TEST_ERROR1 = myErrors.define(1, "MY_TEST_ERROR1", "Custom error 1", { | ||
source: "UK" | ||
const MY_TEST_ERROR1 = myErrors.define(1, 'MY_TEST_ERROR1', 'Custom error 1', { | ||
source: 'UK' | ||
}); | ||
@@ -34,8 +34,8 @@ /** | ||
*/ | ||
const MY_TEST_ERROR2 = myErrors.define(2, "MY_TEST_ERROR2", "Custom error 2", { | ||
"source": "China" | ||
}, [1234], ["HEIHEIHEI"]); | ||
const MY_TEST_ERROR2 = myErrors.define(2, 'MY_TEST_ERROR2', 'Custom error 2', { | ||
'source': 'China' | ||
}); | ||
console.error(JSON.stringify(new MY_TEST_ERROR1({ | ||
"metadata": { | ||
"source": "USA" | ||
'metadata': { | ||
'source': 'USA' | ||
} | ||
@@ -45,11 +45,8 @@ }).toJSON(true), null, 2)); | ||
console.log(MY_TEST_ERROR2.message); | ||
console.log(MY_TEST_ERROR2.aliasCodes); | ||
console.log(MY_TEST_ERROR2.aliases); | ||
console.log(new MY_TEST_ERROR2().metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).aliasCodes); | ||
console.log(new MY_TEST_ERROR2({ 'metadata': { 'source': 'Japan' } }).metadata.source); | ||
} | ||
try { | ||
myErrors.define(2, "MY_TEST_ERROR2", "Custom error 2", { | ||
source: "RU" | ||
myErrors.define(2, 'MY_TEST_ERROR2', 'Custom error 2', { | ||
source: 'RU' | ||
}); | ||
@@ -63,9 +60,9 @@ } | ||
console.log(`Error[MY_TEST_ERROR2] Module: ${MY_TEST_ERROR2.module}`); | ||
console.log(`Error[INVALID_ERROR_NAME] Module: ${errors.get("INVALID_ERROR_NAME").module}`); | ||
console.log(`Error[MY_TEST_ERROR2] Source: ${myErrors.get("MY_TEST_ERROR2").defaultMetadata.source}`); | ||
console.log(`Error[INVALID_ERROR_NAME] Module: ${errors.get('INVALID_ERROR_NAME').module}`); | ||
console.log(`Error[MY_TEST_ERROR2] Source: ${myErrors.get('MY_TEST_ERROR2').defaultMetadata.source}`); | ||
console.log(`Error[1234] Name: ${myErrors.get(1234).name}`); | ||
console.log(`Error[HEIHEIHEI] Name: ${myErrors.get("HEIHEIHEI").name}`); | ||
console.log(`Error[HEIHEIHEI] Name: ${myErrors.get('HEIHEIHEI').name}`); | ||
new MY_TEST_ERROR1().warn(); | ||
myErrors.removeWarningListener(Core.DEFAULT_WARNING_LISTENER_KEY); | ||
myErrors.addWarningListener("hello", (e) => console.warn("A wanring was emitted.")); | ||
myErrors.addWarningListener('hello', (e) => console.warn(`A wanring was emitted: ${e}`)); | ||
new MY_TEST_ERROR1().warn(); | ||
@@ -72,0 +69,0 @@ myErrors.forceWarningAsError(true); |
@@ -18,7 +18,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
(async () => { | ||
console.log(new Date().toISOString()); | ||
const result = await Core.Async.sleep(1000, "cc", 123, true); | ||
const result = await Core.Async.sleep(1000, 'cc', 123, true); | ||
console.log(new Date().toISOString()); | ||
@@ -28,3 +28,3 @@ for (let i = 0; i < result.length; i++) { | ||
} | ||
})(); | ||
})().catch((e) => console.error(e)); | ||
//# sourceMappingURL=02-async.sleep.js.map |
@@ -18,3 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
@@ -27,8 +27,8 @@ async function makePromise(ms) { | ||
} | ||
throw new Error("Failed."); | ||
throw new Error('Failed.'); | ||
} | ||
(async () => { | ||
const result = await Core.Async.multiTasks(Array(10).fill(0).map((x) => Math.floor(Math.random() * 1000)).map(makePromise)); | ||
const result = await Core.Async.multiTasks(Array(10).fill(0).map(() => Math.floor(Math.random() * 1000)).map(makePromise)); | ||
console.log(JSON.stringify(result, null, 2)); | ||
})(); | ||
})().catch((e) => console.error(e)); | ||
//# sourceMappingURL=03-async.multiTasks.js.map |
@@ -18,17 +18,17 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
(async () => { | ||
let ret = new Core.TimeoutPromise(100, new Error("TIMEOUT"), true, function (result) { | ||
let ret = new Core.TimeoutPromise(100, new Error('TIMEOUT'), true, function (result) { | ||
if (result.value) { | ||
console.log(`Timeout Result:`, result.value); | ||
console.log('Timeout Result:', result.value); | ||
} | ||
else if (result.error) { | ||
console.error(`Timeout Error:`, result.error); | ||
console.error('Timeout Error:', result.error); | ||
} | ||
}); | ||
setTimeout(ret.resolve, 300, "hello"); | ||
setTimeout(ret.resolve, 300, 'hello'); | ||
try { | ||
await ret.promise; | ||
console.log("Not timeout."); | ||
console.log('Not timeout.'); | ||
} | ||
@@ -38,3 +38,3 @@ catch (e) { | ||
} | ||
})(); | ||
})().catch((e) => console.error(e)); | ||
//# sourceMappingURL=04-class.TimeoutPromise.js.map |
@@ -18,8 +18,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
const Core = require("../lib"); | ||
Core.Async.nextTick(function (a) { | ||
console.log(a); | ||
}, "world"); | ||
console.log("hello"); | ||
}, 'world'); | ||
console.log('hello'); | ||
//# sourceMappingURL=05-async.nextTick.js.map |
@@ -18,21 +18,21 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
const TESTS = [ | ||
"", | ||
"localhost", | ||
"127.0.0.1", | ||
"a.com", | ||
".", | ||
"com.", | ||
".com", | ||
"-", | ||
"_", | ||
"g_", | ||
"g-", | ||
"c+", | ||
"c!", | ||
"a.b.c.d", | ||
"www.google.com", | ||
".google.com" | ||
'', | ||
'localhost', | ||
'127.0.0.1', | ||
'a.com', | ||
'.', | ||
'com.', | ||
'.com', | ||
'-', | ||
'_', | ||
'g_', | ||
'g-', | ||
'c+', | ||
'c!', | ||
'a.b.c.d', | ||
'www.google.com', | ||
'.google.com' | ||
]; | ||
@@ -42,8 +42,8 @@ const MAX_LENGTH = Math.max(...TESTS.map((x) => x.length)) + 4; | ||
if (Core.Validators.isDomain(item)) { | ||
console.info(`${item.padEnd(MAX_LENGTH, " ")} -> Yes`); | ||
console.info(`${item.padEnd(MAX_LENGTH, ' ')} -> Yes`); | ||
} | ||
else { | ||
console.error(`${item.padEnd(MAX_LENGTH, " ")} -> No`); | ||
console.error(`${item.padEnd(MAX_LENGTH, ' ')} -> No`); | ||
} | ||
} | ||
//# sourceMappingURL=06-validators.isDomain.js.map |
@@ -18,26 +18,26 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
const TESTS = [ | ||
"@", | ||
"@localhost", | ||
"@127.0.0.1", | ||
"@a.com", | ||
"@.", | ||
"@com.", | ||
"@.com", | ||
"@-", | ||
"_@", | ||
"g@", | ||
"g@-", | ||
"cccc", | ||
"cc..cc@localhost", | ||
"a.@b.c.d", | ||
"admin@www.google.com", | ||
"admin@.google.com", | ||
"fenying@litert.org", | ||
"c.ff@gmail.com", | ||
"hello@gmail.com", | ||
"hello..world@gmail.com", | ||
"ggg-c@cc.ca" | ||
'@', | ||
'@localhost', | ||
'@127.0.0.1', | ||
'@a.com', | ||
'@.', | ||
'@com.', | ||
'@.com', | ||
'@-', | ||
'_@', | ||
'g@', | ||
'g@-', | ||
'cccc', | ||
'cc..cc@localhost', | ||
'a.@b.c.d', | ||
'admin@www.google.com', | ||
'admin@.google.com', | ||
'fenying@litert.org', | ||
'c.ff@gmail.com', | ||
'hello@gmail.com', | ||
'hello..world@gmail.com', | ||
'ggg-c@cc.ca' | ||
]; | ||
@@ -47,8 +47,8 @@ const MAX_LENGTH = Math.max(...TESTS.map((x) => x.length)) + 4; | ||
if (Core.Validators.isEMailAddress(item)) { | ||
console.info(`${item.padEnd(MAX_LENGTH, " ")} -> Yes`); | ||
console.info(`${item.padEnd(MAX_LENGTH, ' ')} -> Yes`); | ||
} | ||
else { | ||
console.error(`${item.padEnd(MAX_LENGTH, " ")} -> No`); | ||
console.error(`${item.padEnd(MAX_LENGTH, ' ')} -> No`); | ||
} | ||
} | ||
//# sourceMappingURL=07-validators.isEMailAddress.js.map |
@@ -18,3 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
const Core = require("../lib"); | ||
@@ -26,5 +26,5 @@ (async () => { | ||
*/ | ||
setTimeout(rp.resolve, 1000, 123456); | ||
setTimeout(rp.resolve, 1000, 'hello'); | ||
console.log(await rp.promise); | ||
})(); | ||
})().catch((e) => console.error(e)); | ||
//# sourceMappingURL=08-class.RawPromise.js.map |
@@ -18,2 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RawPromise = void 0; | ||
/** | ||
@@ -20,0 +21,0 @@ * This class helps simplify the usage of Promise. |
@@ -16,9 +16,9 @@ /** | ||
*/ | ||
import { RawPromise } from "./class.RawPromise"; | ||
import { RawPromise } from './class.RawPromise'; | ||
export declare type ITimeoutResult<T, E> = { | ||
"error": null; | ||
"value"?: T; | ||
'error': null; | ||
'value'?: T; | ||
} | { | ||
"error": E; | ||
"value": void; | ||
'error': E; | ||
'value': void; | ||
}; | ||
@@ -25,0 +25,0 @@ export declare type IPromiseTimeoutResultHandler<T, E> = (result: ITimeoutResult<T, E>) => void; |
@@ -18,2 +18,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TimeoutPromise = void 0; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const class_RawPromise_1 = require("./class.RawPromise"); | ||
@@ -56,6 +58,8 @@ /** | ||
if (null === this._timer) { | ||
this._handleTimeout && this._handleTimeout({ | ||
"error": e, | ||
"value": undefined | ||
}); | ||
if (this._handleTimeout) { | ||
this._handleTimeout({ | ||
'error': e, | ||
'value': undefined | ||
}); | ||
} | ||
return; | ||
@@ -72,6 +76,8 @@ } | ||
if (null === this._timer) { | ||
this._handleTimeout && this._handleTimeout({ | ||
"error": null, | ||
"value": data | ||
}); | ||
if (this._handleTimeout) { | ||
this._handleTimeout({ | ||
'error': null, | ||
'value': data | ||
}); | ||
} | ||
return; | ||
@@ -78,0 +84,0 @@ } |
@@ -27,6 +27,2 @@ /** | ||
/** | ||
* The other code of this error. | ||
*/ | ||
aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -37,6 +33,2 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -74,6 +66,2 @@ * error. | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -84,6 +72,2 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -139,6 +123,2 @@ * error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The default description of the error. | ||
@@ -152,6 +132,2 @@ */ | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The name of module thats emit this error. | ||
@@ -175,2 +151,3 @@ */ | ||
readonly module: string; | ||
codeIndex: number; | ||
/** | ||
@@ -184,6 +161,4 @@ * Define a new error type. | ||
* @param metadata The metadata of this new error. | ||
* @param aliasCodes The alias codes of this new error. | ||
* @param aliases The alias names of this new error. | ||
*/ | ||
define<M2 extends M = M>(code: number | null, name: string, message: string, metadata: M2, aliasCodes?: number[], aliases?: string[]): IErrorConstructor<M2>; | ||
define(code: number | null, name: string, message: string, metadata: M): IErrorConstructor<M>; | ||
/** | ||
@@ -194,3 +169,3 @@ * Get the error constructor by its name or code. | ||
*/ | ||
get<M2 extends M = M>(identity: string | number): IErrorConstructor<M2>; | ||
get(identity: string | number): IErrorConstructor<M>; | ||
/** | ||
@@ -202,9 +177,2 @@ * Check if an error belongs to an error type defined in this hub. | ||
*/ | ||
is<M2 extends M = M>(e: any, id?: string | number): e is IError<M2>; | ||
/** | ||
* Check if an error belongs to an error type defined in this hub. | ||
* | ||
* @param e The error to be checked. | ||
* @param id The name or code of error type to be checked. | ||
*/ | ||
is(e: any, id?: string | number): e is IError<M>; | ||
@@ -242,3 +210,3 @@ /** | ||
export declare const DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener"; | ||
export declare const DEFAULT_WARNING_LISTENER: (e: IError<Record<string, any>>) => void; | ||
export declare const DEFAULT_WARNING_LISTENER: (e: IError) => void; | ||
/** | ||
@@ -257,3 +225,3 @@ * Create a new error hub that has a standalone namespace of error types. | ||
*/ | ||
export declare function isError<M extends DefaultMetadataType = DefaultMetadataType>(e: any): e is IError<M>; | ||
export declare function isError<M extends DefaultMetadataType = DefaultMetadataType>(e: unknown): e is IError<M>; | ||
//# sourceMappingURL=Error.d.ts.map |
137
lib/Error.js
@@ -18,2 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isError = exports.getDefaultErrorHub = exports.createErrorHub = exports.DEFAULT_WARNING_LISTENER = exports.DEFAULT_WARNING_LISTENER_KEY = exports.DEFAULT_ERROR_HUB_MODULE = void 0; | ||
const BaseError = (function () { | ||
@@ -52,17 +53,7 @@ let THE_CONSTRUCTOR = ` | ||
"value": {} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliasCodes | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliases | ||
} | ||
}); | ||
`; | ||
if ("captureStackTrace" in Error) { | ||
THE_CONSTRUCTOR += `Error.captureStackTrace(this, this.constructor);`; | ||
if ('captureStackTrace' in Error) { | ||
THE_CONSTRUCTOR += 'Error.captureStackTrace(this, this.constructor);'; | ||
} | ||
@@ -76,4 +67,5 @@ else { | ||
} | ||
let ret = Function("code", "name", "message", "metadata", "moduleName", "aliasCodes", "aliases", THE_CONSTRUCTOR); | ||
let ret = Function('code', 'name', 'message', 'metadata', 'moduleName', THE_CONSTRUCTOR); | ||
ret.prototype.getStackAsArray = function () { | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
return this.stack.split(/\n\s+at /).slice(1); | ||
@@ -83,18 +75,14 @@ }; | ||
return withStack ? { | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"name": this.name, | ||
"aliases": this.aliases, | ||
"stack": this.getStackAsArray() | ||
'code': this.code, | ||
'message': this.message, | ||
'metadata': this.metadata, | ||
'module': this.module, | ||
'name': this.name, | ||
'stack': this.getStackAsArray() | ||
} : { | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"aliases": this.aliases, | ||
"name": this.name | ||
'code': this.code, | ||
'message': this.message, | ||
'metadata': this.metadata, | ||
'module': this.module, | ||
'name': this.name | ||
}; | ||
@@ -105,3 +93,3 @@ }; | ||
Call Stack: | ||
${this.getStackAsArray().join("\n ")}`; | ||
${this.getStackAsArray().join('\n ')}`; | ||
}; | ||
@@ -120,4 +108,4 @@ return ret; | ||
module: moduleName, | ||
baseError: (Function("BaseError", `class __ extends BaseError { | ||
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) { | ||
baseError: (Function('BaseError', `class __ extends BaseError { | ||
constructor(code, name, message, metadata, moduleName) { | ||
super( | ||
@@ -128,5 +116,3 @@ code, | ||
metadata, | ||
moduleName, | ||
aliasCodes, | ||
aliases | ||
moduleName | ||
); | ||
@@ -138,2 +124,8 @@ }; | ||
} | ||
get codeIndex() { | ||
return HUB_SECRETS.get(this).counter; | ||
} | ||
set codeIndex(v) { | ||
HUB_SECRETS.get(this).counter = v; | ||
} | ||
removeWarningListener(key) { | ||
@@ -146,3 +138,3 @@ delete HUB_SECRETS.get(this).warningListeners[key]; | ||
if (_this.warningListeners[key]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_WARNING_LISTENER"); | ||
const TheError = DEFAULT_HUB.get('DUPLICATED_WARNING_LISTENER'); | ||
throw new TheError({ | ||
@@ -174,3 +166,3 @@ message: `Duplicated key of warning listener: ${JSON.stringify(name)}.` | ||
const _this = HUB_SECRETS.get(this); | ||
if (typeof id !== "undefined") { | ||
if (typeof id !== 'undefined') { | ||
return (!!_this.errors[id]) && (e instanceof _this.errors[id]); | ||
@@ -182,6 +174,6 @@ } | ||
} | ||
define(code, name, message, metadata, aliasCodes = [], aliases = []) { | ||
define(code, name, message, metadata) { | ||
const _this = HUB_SECRETS.get(this); | ||
if (!/^[a-z]\w+$/i.test(name)) { | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_NAME"); | ||
const TheError = DEFAULT_HUB.get('INVALID_ERROR_NAME'); | ||
throw new TheError({ | ||
@@ -195,3 +187,3 @@ message: `Invalid name ${JSON.stringify(name)} for error definition.` | ||
else if (!Number.isSafeInteger(code)) { | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_CODE"); | ||
const TheError = DEFAULT_HUB.get('INVALID_ERROR_CODE'); | ||
throw new TheError({ | ||
@@ -205,3 +197,3 @@ message: `Invalid code ${JSON.stringify(code)} for error definition.` | ||
if (_this.errors[name]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
const TheError = DEFAULT_HUB.get('DUPLICATED_ERROR_NAME'); | ||
throw new TheError({ | ||
@@ -211,14 +203,4 @@ message: `The name ${JSON.stringify(name)} of new error already exists.` | ||
} | ||
if (aliases.length) { | ||
for (const alias of aliases) { | ||
if (_this.errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `The name ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
if (_this.errors[code]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
const TheError = DEFAULT_HUB.get('DUPLICATED_ERROR_CODE'); | ||
throw new TheError({ | ||
@@ -228,13 +210,3 @@ message: `The code ${JSON.stringify(code)} of new error already exists.` | ||
} | ||
if (aliasCodes.length) { | ||
for (const alias of aliasCodes) { | ||
if (_this.errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `The code ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
_this.errors[code] = _this.errors[name] = (Function("BaseError", "metadata", "hub", `class ${name} extends BaseError { | ||
_this.errors[code] = _this.errors[name] = (Function('BaseError', 'metadata', 'hub', `class ${name} extends BaseError { | ||
constructor(opts = {}) { | ||
@@ -246,5 +218,3 @@ super( | ||
{ ...metadata, ...opts.metadata }, | ||
${JSON.stringify(_this.module)}, | ||
${JSON.stringify(aliasCodes)}, | ||
${JSON.stringify(aliases)} | ||
${JSON.stringify(_this.module)} | ||
); | ||
@@ -281,12 +251,2 @@ }; | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliases)} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliasCodes)} | ||
}, | ||
"defaultMetadata": { | ||
@@ -300,12 +260,2 @@ "writable": false, | ||
return ${name};`))(_this.baseError, metadata, this); | ||
if (aliasCodes) { | ||
for (const alias of aliasCodes) { | ||
_this.errors[alias] = _this.errors[code]; | ||
} | ||
} | ||
if (aliases) { | ||
for (const alias of aliases) { | ||
_this.errors[alias] = _this.errors[code]; | ||
} | ||
} | ||
return _this.errors[code]; | ||
@@ -320,6 +270,5 @@ } | ||
*/ | ||
exports.DEFAULT_ERROR_HUB_MODULE = "unknown"; | ||
exports.DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener"; | ||
exports.DEFAULT_ERROR_HUB_MODULE = 'unknown'; | ||
exports.DEFAULT_WARNING_LISTENER_KEY = 'litert:errors:hub:default-warning-listener'; | ||
exports.DEFAULT_WARNING_LISTENER = function (e) { | ||
// tslint:disable-next-line: no-console | ||
console.warn(`WARNING: A warning was emitted, and it may become an error in the future versions. | ||
@@ -335,8 +284,8 @@ ${e}`); | ||
exports.createErrorHub = createErrorHub; | ||
DEFAULT_HUB = createErrorHub("@litert/core"); | ||
DEFAULT_HUB.define(null, "INVALID_ERROR_NAME", `Invalid name for error definition.`, {}); | ||
DEFAULT_HUB.define(null, "INVALID_ERROR_CODE", `Invalid code for error definition.`, {}); | ||
DEFAULT_HUB.define(null, "DUPLICATED_ERROR_NAME", `The name of new error already exists.`, {}); | ||
DEFAULT_HUB.define(null, "DUPLICATED_ERROR_CODE", `The code of new error already exists.`, {}); | ||
DEFAULT_HUB.define(null, "DUPLICATED_WARNING_LISTENER", `The key of warning listener already exists.`, {}); | ||
DEFAULT_HUB = createErrorHub('@litert/core'); | ||
DEFAULT_HUB.define(null, 'INVALID_ERROR_NAME', 'Invalid name for error definition.', {}); | ||
DEFAULT_HUB.define(null, 'INVALID_ERROR_CODE', 'Invalid code for error definition.', {}); | ||
DEFAULT_HUB.define(null, 'DUPLICATED_ERROR_NAME', 'The name of new error already exists.', {}); | ||
DEFAULT_HUB.define(null, 'DUPLICATED_ERROR_CODE', 'The code of new error already exists.', {}); | ||
DEFAULT_HUB.define(null, 'DUPLICATED_WARNING_LISTENER', 'The key of warning listener already exists.', {}); | ||
/** | ||
@@ -343,0 +292,0 @@ * Get the default hub of errors. |
@@ -20,8 +20,8 @@ /** | ||
export declare type Nullable<T> = T | null; | ||
export * from "./class.RawPromise"; | ||
export * from "./class.TimeoutPromise"; | ||
export * from "./Error"; | ||
import * as Async from "./utilities.async"; | ||
import * as Validators from "./utilities.validators"; | ||
export * from './class.RawPromise'; | ||
export * from './class.TimeoutPromise'; | ||
export * from './Error'; | ||
import * as Async from './utilities.async'; | ||
import * as Validators from './utilities.validators'; | ||
export { Async, Validators }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -17,9 +17,17 @@ "use strict"; | ||
*/ | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./class.RawPromise")); | ||
__export(require("./class.TimeoutPromise")); | ||
__export(require("./Error")); | ||
exports.Validators = exports.Async = void 0; | ||
__exportStar(require("./class.RawPromise"), exports); | ||
__exportStar(require("./class.TimeoutPromise"), exports); | ||
__exportStar(require("./Error"), exports); | ||
const Async = require("./utilities.async"); | ||
@@ -26,0 +34,0 @@ exports.Async = Async; |
@@ -18,2 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.multiTasks = exports.nextTick = exports.sleep = void 0; | ||
/** | ||
@@ -34,3 +35,3 @@ * Sleep for determined time and then wake up. | ||
*/ | ||
exports.nextTick = typeof setImmediate === "function" ? | ||
exports.nextTick = typeof setImmediate === 'function' ? | ||
setImmediate : function (fn, ...args) { | ||
@@ -55,3 +56,3 @@ setTimeout(fn, 0, ...args); | ||
if (++done === tasks.length) { | ||
setTimeout(resolve, 0, ret); | ||
resolve(ret); | ||
} | ||
@@ -64,3 +65,3 @@ }).catch(function (e) { | ||
if (++done === tasks.length) { | ||
setTimeout(resolve, 0, ret); | ||
resolve(ret); | ||
} | ||
@@ -67,0 +68,0 @@ }); |
@@ -18,6 +18,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isEMailAddress = exports.isDomain = void 0; | ||
const DOMAIN_REGEXP = /^[a-z0-9][-a-z0-9]{0,62}(\.[a-z0-9][-a-z0-9]{0,62})*$/i; | ||
const DOMAIN_MIN_LENGTH = 1; | ||
const DOMAIN_MAX_LENGTH = 255; | ||
const EMAIL_REGEXP = /^[-+_a-z0-9][-+_\.a-z0-9]{0,62}@[a-z0-9][-a-z0-9]{0,62}(\.[a-z0-9][-a-z0-9]{0,62})*$/i; | ||
const EMAIL_REGEXP = /^[-+_a-z0-9][-+_.a-z0-9]{0,62}@[a-z0-9][-a-z0-9]{0,62}(\.[a-z0-9][-a-z0-9]{0,62})*$/i; | ||
const EMAIL_MAX_LENGTH = 255; | ||
const EMAIL_MIN_LENGTH = 1; | ||
/** | ||
@@ -29,3 +32,3 @@ * Validate if the input string is a domain. | ||
function isDomain(domain) { | ||
return domain.length > 0 && | ||
return domain.length >= DOMAIN_MIN_LENGTH && | ||
domain.length < DOMAIN_MAX_LENGTH && | ||
@@ -43,6 +46,6 @@ DOMAIN_REGEXP.test(domain); | ||
function isEMailAddress(email) { | ||
return email.length > 0 && | ||
return email.length >= EMAIL_MIN_LENGTH && | ||
email.length < EMAIL_MAX_LENGTH && | ||
email.indexOf("..") === -1 && | ||
email.indexOf(".@") === -1 && | ||
!email.includes('..') && | ||
!email.includes('.@') && | ||
EMAIL_REGEXP.test(email); | ||
@@ -49,0 +52,0 @@ } |
{ | ||
"name": "@litert/core", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "The core of LiteRT.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"prepare": "npm run rebuild", | ||
"prepublishOnly": "npm run rebuild", | ||
"build": "echo Using TypeScript && tsc -v && tsc -p .", | ||
@@ -13,3 +13,4 @@ "build-watch": "echo Using TypeScript && tsc -v && tsc -w -p .", | ||
"clean": "rm -rf lib examples", | ||
"lint": "tslint -p . -c tslint.json" | ||
"typecheck": "tsc -p . --noEmit", | ||
"lint": "eslint --ext ts src" | ||
}, | ||
@@ -34,4 +35,18 @@ "repository": { | ||
"devDependencies": { | ||
"typescript": "^3.2.2" | ||
} | ||
"@typescript-eslint/eslint-plugin": "^2.34.0", | ||
"@typescript-eslint/parser": "^2.34.0", | ||
"eslint": "^6.8.0", | ||
"typescript": "^4.0.2", | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-conventional": "^11.0.0", | ||
"@types/node": "^14.11.1", | ||
"husky": "^4.3.0" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | ||
"pre-commit": "npm run lint && npm run typecheck" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
@@ -17,28 +17,22 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
const errors = Core.getDefaultErrorHub(); | ||
const TEST_ERROR = errors.define(123, "TEST_ERROR", "This is a test error.", {}); | ||
const TEST_ERROR = errors.define(123, 'TEST_ERROR', 'This is a test error.', {}); | ||
console.error(JSON.stringify(new TEST_ERROR().toJSON(true), null, 2)); | ||
interface IMyErrorMetadata { | ||
const myErrors = Core.createErrorHub('@litert/core#error-example'); | ||
source: string; | ||
} | ||
const myErrors = Core.createErrorHub("@litert/core#error-example"); | ||
/** | ||
* Type 1 of error. | ||
*/ | ||
const MY_TEST_ERROR1 = myErrors.define<IMyErrorMetadata>( | ||
const MY_TEST_ERROR1 = myErrors.define( | ||
1, | ||
"MY_TEST_ERROR1", | ||
"Custom error 1", | ||
'MY_TEST_ERROR1', | ||
'Custom error 1', | ||
{ | ||
source: "UK" | ||
source: 'UK' | ||
} | ||
@@ -50,16 +44,14 @@ ); | ||
*/ | ||
const MY_TEST_ERROR2 = myErrors.define<IMyErrorMetadata>( | ||
const MY_TEST_ERROR2 = myErrors.define( | ||
2, | ||
"MY_TEST_ERROR2", | ||
"Custom error 2", | ||
'MY_TEST_ERROR2', | ||
'Custom error 2', | ||
{ | ||
"source": "China" | ||
}, | ||
[1234], | ||
["HEIHEIHEI"] | ||
'source': 'China' | ||
} | ||
); | ||
console.error(JSON.stringify(new MY_TEST_ERROR1({ | ||
"metadata": { | ||
"source": "USA" | ||
'metadata': { | ||
'source': 'USA' | ||
} | ||
@@ -71,7 +63,4 @@ }).toJSON(true), null, 2)); | ||
console.log(MY_TEST_ERROR2.message); | ||
console.log(MY_TEST_ERROR2.aliasCodes); | ||
console.log(MY_TEST_ERROR2.aliases); | ||
console.log(new MY_TEST_ERROR2().metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).metadata.source); | ||
console.log(new MY_TEST_ERROR2({ "metadata": { "source": "Japan" } }).aliasCodes); | ||
console.log(new MY_TEST_ERROR2({ 'metadata': { 'source': 'Japan' } }).metadata.source); | ||
} | ||
@@ -81,8 +70,8 @@ | ||
myErrors.define<IMyErrorMetadata>( | ||
myErrors.define( | ||
2, | ||
"MY_TEST_ERROR2", | ||
"Custom error 2", | ||
'MY_TEST_ERROR2', | ||
'Custom error 2', | ||
{ | ||
source: "RU" | ||
source: 'RU' | ||
} | ||
@@ -102,9 +91,9 @@ ); | ||
console.log(`Error[INVALID_ERROR_NAME] Module: ${errors.get("INVALID_ERROR_NAME").module}`); | ||
console.log(`Error[INVALID_ERROR_NAME] Module: ${errors.get('INVALID_ERROR_NAME').module}`); | ||
console.log(`Error[MY_TEST_ERROR2] Source: ${myErrors.get("MY_TEST_ERROR2").defaultMetadata.source}`); | ||
console.log(`Error[MY_TEST_ERROR2] Source: ${myErrors.get('MY_TEST_ERROR2').defaultMetadata.source}`); | ||
console.log(`Error[1234] Name: ${myErrors.get(1234).name}`); | ||
console.log(`Error[HEIHEIHEI] Name: ${myErrors.get("HEIHEIHEI").name}`); | ||
console.log(`Error[HEIHEIHEI] Name: ${myErrors.get('HEIHEIHEI').name}`); | ||
@@ -115,3 +104,3 @@ new MY_TEST_ERROR1().warn(); | ||
myErrors.addWarningListener("hello", (e) => console.warn("A wanring was emitted.")); | ||
myErrors.addWarningListener('hello', (e) => console.warn(`A wanring was emitted: ${e}`)); | ||
@@ -118,0 +107,0 @@ new MY_TEST_ERROR1().warn(); |
@@ -17,11 +17,11 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from "../lib"; | ||
import * as Core from '../lib'; | ||
(async () => { | ||
(async (): Promise<void> => { | ||
console.log(new Date().toISOString()); | ||
const result = await Core.Async.sleep(1000, "cc", 123, true); | ||
const result = await Core.Async.sleep(1000, 'cc', 123, true); | ||
@@ -35,2 +35,2 @@ console.log(new Date().toISOString()); | ||
})(); | ||
})().catch((e) => console.error(e)); |
@@ -17,6 +17,5 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
async function makePromise(ms: number): Promise<number> { | ||
@@ -33,10 +32,10 @@ | ||
throw new Error("Failed."); | ||
throw new Error('Failed.'); | ||
} | ||
(async () => { | ||
(async (): Promise<void> => { | ||
const result = await Core.Async.multiTasks( | ||
Array(10).fill(0).map( | ||
(x) => Math.floor(Math.random() * 1000) | ||
() => Math.floor(Math.random() * 1000) | ||
).map(makePromise) | ||
@@ -47,2 +46,2 @@ ); | ||
})(); | ||
})().catch((e) => console.error(e)); |
@@ -17,11 +17,10 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
(async (): Promise<void> => { | ||
(async () => { | ||
let ret = new Core.TimeoutPromise<string, Error>( | ||
100, | ||
new Error("TIMEOUT"), | ||
new Error('TIMEOUT'), | ||
true, | ||
@@ -32,7 +31,7 @@ function(result): void { | ||
console.log(`Timeout Result:`, result.value); | ||
console.log('Timeout Result:', result.value); | ||
} | ||
else if (result.error) { | ||
console.error(`Timeout Error:`, result.error); | ||
console.error('Timeout Error:', result.error); | ||
} | ||
@@ -42,3 +41,3 @@ } | ||
setTimeout(ret.resolve, 300, "hello"); | ||
setTimeout(ret.resolve, 300, 'hello'); | ||
@@ -49,3 +48,3 @@ try { | ||
console.log("Not timeout."); | ||
console.log('Not timeout.'); | ||
} | ||
@@ -57,2 +56,2 @@ catch (e) { | ||
})(); | ||
})().catch((e) => console.error(e)); |
@@ -17,6 +17,4 @@ /** | ||
// tslint:disable:no-console | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
Core.Async.nextTick(function(a: string): void { | ||
@@ -26,4 +24,4 @@ | ||
}, "world"); | ||
}, 'world'); | ||
console.log("hello"); | ||
console.log('hello'); |
@@ -17,23 +17,22 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
const TESTS: string[] = [ | ||
"", | ||
"localhost", | ||
"127.0.0.1", | ||
"a.com", | ||
".", | ||
"com.", | ||
".com", | ||
"-", | ||
"_", | ||
"g_", | ||
"g-", | ||
"c+", | ||
"c!", | ||
"a.b.c.d", | ||
"www.google.com", | ||
".google.com" | ||
'', | ||
'localhost', | ||
'127.0.0.1', | ||
'a.com', | ||
'.', | ||
'com.', | ||
'.com', | ||
'-', | ||
'_', | ||
'g_', | ||
'g-', | ||
'c+', | ||
'c!', | ||
'a.b.c.d', | ||
'www.google.com', | ||
'.google.com' | ||
]; | ||
@@ -47,8 +46,8 @@ | ||
console.info(`${item.padEnd(MAX_LENGTH, " ")} -> Yes`); | ||
console.info(`${item.padEnd(MAX_LENGTH, ' ')} -> Yes`); | ||
} | ||
else { | ||
console.error(`${item.padEnd(MAX_LENGTH, " ")} -> No`); | ||
console.error(`${item.padEnd(MAX_LENGTH, ' ')} -> No`); | ||
} | ||
} |
@@ -17,28 +17,27 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
const TESTS: string[] = [ | ||
"@", | ||
"@localhost", | ||
"@127.0.0.1", | ||
"@a.com", | ||
"@.", | ||
"@com.", | ||
"@.com", | ||
"@-", | ||
"_@", | ||
"g@", | ||
"g@-", | ||
"cccc", | ||
"cc..cc@localhost", | ||
"a.@b.c.d", | ||
"admin@www.google.com", | ||
"admin@.google.com", | ||
"fenying@litert.org", | ||
"c.ff@gmail.com", | ||
"hello@gmail.com", | ||
"hello..world@gmail.com", | ||
"ggg-c@cc.ca" | ||
'@', | ||
'@localhost', | ||
'@127.0.0.1', | ||
'@a.com', | ||
'@.', | ||
'@com.', | ||
'@.com', | ||
'@-', | ||
'_@', | ||
'g@', | ||
'g@-', | ||
'cccc', | ||
'cc..cc@localhost', | ||
'a.@b.c.d', | ||
'admin@www.google.com', | ||
'admin@.google.com', | ||
'fenying@litert.org', | ||
'c.ff@gmail.com', | ||
'hello@gmail.com', | ||
'hello..world@gmail.com', | ||
'ggg-c@cc.ca' | ||
]; | ||
@@ -52,8 +51,8 @@ | ||
console.info(`${item.padEnd(MAX_LENGTH, " ")} -> Yes`); | ||
console.info(`${item.padEnd(MAX_LENGTH, ' ')} -> Yes`); | ||
} | ||
else { | ||
console.error(`${item.padEnd(MAX_LENGTH, " ")} -> No`); | ||
console.error(`${item.padEnd(MAX_LENGTH, ' ')} -> No`); | ||
} | ||
} |
@@ -17,8 +17,7 @@ /** | ||
// tslint:disable:no-console | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import * as Core from '../lib'; | ||
import * as Core from "../lib"; | ||
(async (): Promise<void> => { | ||
(async () => { | ||
let rp = new Core.RawPromise<number>(); | ||
@@ -29,6 +28,6 @@ | ||
*/ | ||
setTimeout(rp.resolve, 1000, 123456); | ||
setTimeout(rp.resolve, 1000, 'hello'); | ||
console.log(await rp.promise); | ||
})(); | ||
})().catch((e) => console.error(e)); |
@@ -17,15 +17,16 @@ /** | ||
import { RawPromise, IPromiseRejector, IPromiseResolver } from "./class.RawPromise"; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { RawPromise, IPromiseRejector, IPromiseResolver } from './class.RawPromise'; | ||
export type ITimeoutResult<T, E> = { | ||
"error": null; | ||
'error': null; | ||
"value"?: T; | ||
'value'?: T; | ||
} | { | ||
"error": E; | ||
'error': E; | ||
"value": void; | ||
'value': void; | ||
}; | ||
@@ -41,3 +42,3 @@ | ||
export class TimeoutPromise<T = any, E = Error> | ||
extends RawPromise<T, E> { | ||
extends RawPromise<T, E> { | ||
@@ -103,7 +104,10 @@ private _timer!: number | null; | ||
this._handleTimeout && this._handleTimeout({ | ||
"error": e, | ||
"value": undefined | ||
}); | ||
if (this._handleTimeout) { | ||
this._handleTimeout({ | ||
'error': e, | ||
'value': undefined | ||
}); | ||
} | ||
return; | ||
@@ -125,7 +129,10 @@ } | ||
this._handleTimeout && this._handleTimeout({ | ||
"error": null, | ||
"value": data | ||
}); | ||
if (this._handleTimeout) { | ||
this._handleTimeout({ | ||
'error': null, | ||
'value': data | ||
}); | ||
} | ||
return; | ||
@@ -132,0 +139,0 @@ } |
@@ -17,2 +17,3 @@ /** | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export type DefaultMetadataType = Record<string, any>; | ||
@@ -32,7 +33,2 @@ | ||
/** | ||
* The other code of this error. | ||
*/ | ||
aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -44,7 +40,2 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -89,7 +80,2 @@ * error. | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The string-code of an error, as the name, used to identify the type or | ||
@@ -101,7 +87,2 @@ * the reason of the error. | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The detail description of an error, used to describe the details of the | ||
@@ -158,3 +139,3 @@ * error. | ||
moduleName?: string | ||
): IError<DefaultMetadataType>; | ||
): IError; | ||
@@ -187,7 +168,2 @@ name: string; | ||
/** | ||
* The other names of this error. | ||
*/ | ||
readonly aliases: string[]; | ||
/** | ||
* The default description of the error. | ||
@@ -203,7 +179,2 @@ */ | ||
/** | ||
* The other code of this error. | ||
*/ | ||
readonly aliasCodes: number[]; | ||
/** | ||
* The name of module thats emit this error. | ||
@@ -253,12 +224,2 @@ */ | ||
"value": {} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliasCodes | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": aliases | ||
} | ||
@@ -268,5 +229,5 @@ }); | ||
if ("captureStackTrace" in Error) { | ||
if ('captureStackTrace' in Error) { | ||
THE_CONSTRUCTOR += `Error.captureStackTrace(this, this.constructor);`; | ||
THE_CONSTRUCTOR += 'Error.captureStackTrace(this, this.constructor);'; | ||
} | ||
@@ -283,14 +244,13 @@ else { | ||
let ret = Function( | ||
"code", | ||
"name", | ||
"message", | ||
"metadata", | ||
"moduleName", | ||
"aliasCodes", | ||
"aliases", | ||
'code', | ||
'name', | ||
'message', | ||
'metadata', | ||
'moduleName', | ||
THE_CONSTRUCTOR | ||
); | ||
ret.prototype.getStackAsArray = function(this: IError<DefaultMetadataType>): string[] { | ||
ret.prototype.getStackAsArray = function(this: IError): string[] { | ||
// eslint-disable-next-line @typescript-eslint/no-magic-numbers | ||
return this.stack.split(/\n\s+at /).slice(1); | ||
@@ -300,3 +260,3 @@ }; | ||
ret.prototype.toJSON = function( | ||
this: IError<DefaultMetadataType>, | ||
this: IError, | ||
withStack?: boolean | ||
@@ -306,26 +266,22 @@ ): IErrorFullData<DefaultMetadataType> | IErrorData<DefaultMetadataType> { | ||
return withStack ? { | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"name": this.name, | ||
"aliases": this.aliases, | ||
"stack": this.getStackAsArray() | ||
'code': this.code, | ||
'message': this.message, | ||
'metadata': this.metadata, | ||
'module': this.module, | ||
'name': this.name, | ||
'stack': this.getStackAsArray() | ||
} : { | ||
"code": this.code, | ||
"aliasCodes": this.aliasCodes, | ||
"message": this.message, | ||
"metadata": this.metadata, | ||
"module": this.module, | ||
"aliases": this.aliases, | ||
"name": this.name | ||
'code': this.code, | ||
'message': this.message, | ||
'metadata': this.metadata, | ||
'module': this.module, | ||
'name': this.name | ||
}; | ||
}; | ||
ret.prototype.toString = function(this: IError<DefaultMetadataType>): string { | ||
ret.prototype.toString = function(this: IError): string { | ||
return `Error #${this.code} (${this.name} from ${this.module}): ${this.message} | ||
Call Stack: | ||
${this.getStackAsArray().join("\n ")}`; | ||
${this.getStackAsArray().join('\n ')}`; | ||
}; | ||
@@ -348,2 +304,4 @@ | ||
codeIndex: number; | ||
/** | ||
@@ -357,13 +315,9 @@ * Define a new error type. | ||
* @param metadata The metadata of this new error. | ||
* @param aliasCodes The alias codes of this new error. | ||
* @param aliases The alias names of this new error. | ||
*/ | ||
define<M2 extends M = M>( | ||
define( | ||
code: number | null, | ||
name: string, | ||
message: string, | ||
metadata: M2, | ||
aliasCodes?: number[], | ||
aliases?: string[] | ||
): IErrorConstructor<M2>; | ||
metadata: M | ||
): IErrorConstructor<M>; | ||
@@ -375,5 +329,3 @@ /** | ||
*/ | ||
get<M2 extends M = M>( | ||
identity: string | number | ||
): IErrorConstructor<M2>; | ||
get(identity: string | number): IErrorConstructor<M>; | ||
@@ -386,10 +338,2 @@ /** | ||
*/ | ||
is<M2 extends M = M>(e: any, id?: string | number): e is IError<M2>; | ||
/** | ||
* Check if an error belongs to an error type defined in this hub. | ||
* | ||
* @param e The error to be checked. | ||
* @param id The name or code of error type to be checked. | ||
*/ | ||
is(e: any, id?: string | number): e is IError<M>; | ||
@@ -458,4 +402,4 @@ | ||
baseError: (Function( | ||
"BaseError", `class __ extends BaseError { | ||
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) { | ||
'BaseError', `class __ extends BaseError { | ||
constructor(code, name, message, metadata, moduleName) { | ||
super( | ||
@@ -466,5 +410,3 @@ code, | ||
metadata, | ||
moduleName, | ||
aliasCodes, | ||
aliases | ||
moduleName | ||
); | ||
@@ -474,9 +416,19 @@ }; | ||
return __;` | ||
))(BaseError, moduleName) as any | ||
))(BaseError, moduleName) | ||
}); | ||
} | ||
public get codeIndex(): number { | ||
return HUB_SECRETS.get(this)!.counter; | ||
} | ||
public set codeIndex(v: number) { | ||
HUB_SECRETS.get(this)!.counter = v; | ||
} | ||
public removeWarningListener(key: string): this { | ||
delete (HUB_SECRETS.get(this) as IErrorHubPrivateData).warningListeners[key]; | ||
delete HUB_SECRETS.get(this)!.warningListeners[key]; | ||
@@ -488,7 +440,7 @@ return this; | ||
const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData; | ||
const _this = HUB_SECRETS.get(this)!; | ||
if (_this.warningListeners[key]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_WARNING_LISTENER"); | ||
const TheError = DEFAULT_HUB.get('DUPLICATED_WARNING_LISTENER'); | ||
@@ -507,3 +459,3 @@ throw new TheError({ | ||
(HUB_SECRETS.get(this) as IErrorHubPrivateData).warningAsError = enabled; | ||
HUB_SECRETS.get(this)!.warningAsError = enabled; | ||
@@ -515,3 +467,3 @@ return this; | ||
const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData; | ||
const _this = HUB_SECRETS.get(this)!; | ||
@@ -533,3 +485,3 @@ if (_this.warningAsError) { | ||
return (HUB_SECRETS.get(this) as IErrorHubPrivateData).module; | ||
return HUB_SECRETS.get(this)!.module; | ||
} | ||
@@ -539,5 +491,5 @@ | ||
const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData; | ||
const _this = HUB_SECRETS.get(this)!; | ||
if (typeof id !== "undefined") { | ||
if (typeof id !== 'undefined') { | ||
@@ -552,16 +504,14 @@ return (!!_this.errors[id]) && (e instanceof _this.errors[id]); | ||
public define<M2 extends M>( | ||
public define( | ||
code: number | null, | ||
name: string, | ||
message: string, | ||
metadata?: M2, | ||
aliasCodes: number[] = [], | ||
aliases: string[] = [] | ||
): IErrorConstructor<M2> { | ||
metadata?: M | ||
): IErrorConstructor<M> { | ||
const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData; | ||
const _this = HUB_SECRETS.get(this)!; | ||
if (!/^[a-z]\w+$/i.test(name)) { | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_NAME"); | ||
const TheError = DEFAULT_HUB.get('INVALID_ERROR_NAME'); | ||
@@ -579,3 +529,3 @@ throw new TheError({ | ||
const TheError = DEFAULT_HUB.get("INVALID_ERROR_CODE"); | ||
const TheError = DEFAULT_HUB.get('INVALID_ERROR_CODE'); | ||
@@ -593,3 +543,3 @@ throw new TheError({ | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
const TheError = DEFAULT_HUB.get('DUPLICATED_ERROR_NAME'); | ||
@@ -601,20 +551,5 @@ throw new TheError({ | ||
if (aliases.length) { | ||
for (const alias of aliases) { | ||
if (_this.errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME"); | ||
throw new TheError({ | ||
message: `The name ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
if (_this.errors[code]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
const TheError = DEFAULT_HUB.get('DUPLICATED_ERROR_CODE'); | ||
@@ -626,19 +561,4 @@ throw new TheError({ | ||
if (aliasCodes.length) { | ||
for (const alias of aliasCodes) { | ||
if (_this.errors[alias]) { | ||
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE"); | ||
throw new TheError({ | ||
message: `The code ${JSON.stringify(alias)} of new error already exists.` | ||
}); | ||
} | ||
} | ||
} | ||
_this.errors[code] = _this.errors[name] = (Function( | ||
"BaseError", "metadata", "hub", `class ${name} extends BaseError { | ||
'BaseError', 'metadata', 'hub', `class ${name} extends BaseError { | ||
constructor(opts = {}) { | ||
@@ -650,5 +570,3 @@ super( | ||
{ ...metadata, ...opts.metadata }, | ||
${JSON.stringify(_this.module)}, | ||
${JSON.stringify(aliasCodes)}, | ||
${JSON.stringify(aliases)} | ||
${JSON.stringify(_this.module)} | ||
); | ||
@@ -685,12 +603,2 @@ }; | ||
}, | ||
"aliases": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliases)} | ||
}, | ||
"aliasCodes": { | ||
"writable": false, | ||
"configurable": false, | ||
"value": ${JSON.stringify(aliasCodes)} | ||
}, | ||
"defaultMetadata": { | ||
@@ -704,28 +612,10 @@ "writable": false, | ||
return ${name};` | ||
))(_this.baseError, metadata, this) as any; | ||
))(_this.baseError, metadata, this); | ||
if (aliasCodes) { | ||
for (const alias of aliasCodes) { | ||
_this.errors[alias] = _this.errors[code]; | ||
} | ||
} | ||
if (aliases) { | ||
for (const alias of aliases) { | ||
_this.errors[alias] = _this.errors[code]; | ||
} | ||
} | ||
return _this.errors[code]; | ||
} | ||
public get<M2 extends M>( | ||
name: string | ||
): IErrorConstructor<M2> { | ||
public get(name: string): IErrorConstructor<M> { | ||
return (HUB_SECRETS.get(this) as IErrorHubPrivateData).errors[name]; | ||
return HUB_SECRETS.get(this)!.errors[name]; | ||
} | ||
@@ -737,9 +627,8 @@ } | ||
*/ | ||
export const DEFAULT_ERROR_HUB_MODULE = "unknown"; | ||
export const DEFAULT_ERROR_HUB_MODULE = 'unknown'; | ||
export const DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener"; | ||
export const DEFAULT_WARNING_LISTENER_KEY = 'litert:errors:hub:default-warning-listener'; | ||
export const DEFAULT_WARNING_LISTENER = function(e: IError): void { | ||
// tslint:disable-next-line: no-console | ||
console.warn(`WARNING: A warning was emitted, and it may become an error in the future versions. | ||
@@ -762,8 +651,8 @@ ${e}`); | ||
DEFAULT_HUB = createErrorHub<DefaultMetadataType>("@litert/core"); | ||
DEFAULT_HUB = createErrorHub<DefaultMetadataType>('@litert/core'); | ||
DEFAULT_HUB.define( | ||
null, | ||
"INVALID_ERROR_NAME", | ||
`Invalid name for error definition.`, | ||
'INVALID_ERROR_NAME', | ||
'Invalid name for error definition.', | ||
{} | ||
@@ -774,4 +663,4 @@ ); | ||
null, | ||
"INVALID_ERROR_CODE", | ||
`Invalid code for error definition.`, | ||
'INVALID_ERROR_CODE', | ||
'Invalid code for error definition.', | ||
{} | ||
@@ -782,4 +671,4 @@ ); | ||
null, | ||
"DUPLICATED_ERROR_NAME", | ||
`The name of new error already exists.`, | ||
'DUPLICATED_ERROR_NAME', | ||
'The name of new error already exists.', | ||
{} | ||
@@ -790,4 +679,4 @@ ); | ||
null, | ||
"DUPLICATED_ERROR_CODE", | ||
`The code of new error already exists.`, | ||
'DUPLICATED_ERROR_CODE', | ||
'The code of new error already exists.', | ||
{} | ||
@@ -798,4 +687,4 @@ ); | ||
null, | ||
"DUPLICATED_WARNING_LISTENER", | ||
`The key of warning listener already exists.`, | ||
'DUPLICATED_WARNING_LISTENER', | ||
'The key of warning listener already exists.', | ||
{} | ||
@@ -819,5 +708,5 @@ ); | ||
M extends DefaultMetadataType = DefaultMetadataType | ||
>(e: any): e is IError<M> { | ||
>(e: unknown): e is IError<M> { | ||
return e instanceof BaseError; | ||
} |
@@ -22,12 +22,12 @@ /** | ||
export * from "./class.RawPromise"; | ||
export * from './class.RawPromise'; | ||
export * from "./class.TimeoutPromise"; | ||
export * from './class.TimeoutPromise'; | ||
export * from "./Error"; | ||
export * from './Error'; | ||
import * as Async from "./utilities.async"; | ||
import * as Async from './utilities.async'; | ||
import * as Validators from "./utilities.validators"; | ||
import * as Validators from './utilities.validators'; | ||
export { Async, Validators }; |
@@ -17,2 +17,3 @@ /** | ||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-magic-numbers */ | ||
export type IWaitResult<T, E> = { | ||
@@ -55,3 +56,3 @@ | ||
...args: T | ||
) => void = typeof setImmediate === "function" ? | ||
) => void = typeof setImmediate === 'function' ? | ||
setImmediate : function(fn: Function, ...args: any[]): void { | ||
@@ -70,3 +71,3 @@ setTimeout(fn, 0, ...args); | ||
return new Promise<any>(function(resolve): void { | ||
return new Promise<Array<IWaitResult<T, E>>>(function(resolve): void { | ||
@@ -88,3 +89,3 @@ let ret: Array<IWaitResult<T, E>> = Array(tasks.length); | ||
setTimeout(resolve, 0, ret); | ||
resolve(ret); | ||
} | ||
@@ -101,3 +102,3 @@ | ||
setTimeout(resolve, 0, ret); | ||
resolve(ret); | ||
} | ||
@@ -104,0 +105,0 @@ }); |
@@ -19,7 +19,9 @@ /** | ||
const DOMAIN_MIN_LENGTH = 1; | ||
const DOMAIN_MAX_LENGTH = 255; | ||
const EMAIL_REGEXP = /^[-+_a-z0-9][-+_\.a-z0-9]{0,62}@[a-z0-9][-a-z0-9]{0,62}(\.[a-z0-9][-a-z0-9]{0,62})*$/i; | ||
const EMAIL_REGEXP = /^[-+_a-z0-9][-+_.a-z0-9]{0,62}@[a-z0-9][-a-z0-9]{0,62}(\.[a-z0-9][-a-z0-9]{0,62})*$/i; | ||
const EMAIL_MAX_LENGTH = 255; | ||
const EMAIL_MIN_LENGTH = 1; | ||
@@ -33,3 +35,3 @@ /** | ||
return domain.length > 0 && | ||
return domain.length >= DOMAIN_MIN_LENGTH && | ||
domain.length < DOMAIN_MAX_LENGTH && | ||
@@ -48,7 +50,7 @@ DOMAIN_REGEXP.test(domain); | ||
return email.length > 0 && | ||
return email.length >= EMAIL_MIN_LENGTH && | ||
email.length < EMAIL_MAX_LENGTH && | ||
email.indexOf("..") === -1 && | ||
email.indexOf(".@") === -1 && | ||
!email.includes('..') && | ||
!email.includes('.@') && | ||
EMAIL_REGEXP.test(email); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
126363
8
2602