@borodindmitriy/base-code
Advanced tools
| export { EventEmitter } from "./classes/EventEmitter"; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var EventEmitter_1 = require("./classes/EventEmitter"); | ||
| exports.EventEmitter = EventEmitter_1.EventEmitter; |
| export declare class EventEmitter { | ||
| private subscriptions; | ||
| readonly listenersCount: number; | ||
| constructor(); | ||
| emit(name: any, payload?: object): void; | ||
| once(name: any, callBack: any): void; | ||
| has(name: any, callBack: any): boolean; | ||
| on(name: any, callBack: any): void; | ||
| off(name: any, callBack: any): void; | ||
| clear(): void; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| class EventEmitter { | ||
| get listenersCount() { | ||
| return this.subscriptions.size; | ||
| } | ||
| constructor() { | ||
| this.subscriptions = new Map(); | ||
| this.emit = this.emit.bind(this); | ||
| this.once = this.once.bind(this); | ||
| this.on = this.on.bind(this); | ||
| this.off = this.off.bind(this); | ||
| this.has = this.has.bind(this); | ||
| this.clear = this.clear.bind(this); | ||
| } | ||
| emit(name, payload) { | ||
| const listeners = this.subscriptions.get(name); | ||
| if (listeners) { | ||
| listeners.forEach((cb) => cb(payload)); | ||
| } | ||
| } | ||
| once(name, callBack) { | ||
| const cb = (...args) => { | ||
| callBack(...args); | ||
| this.off(name, cb); | ||
| }; | ||
| this.on(name, cb); | ||
| } | ||
| has(name, callBack) { | ||
| const listeners = this.subscriptions.get(name); | ||
| if (listeners) { | ||
| return listeners.has(callBack); | ||
| } | ||
| return false; | ||
| } | ||
| on(name, callBack) { | ||
| const listeners = this.subscriptions.get(name); | ||
| if (listeners) { | ||
| if (!listeners.has(callBack)) { | ||
| listeners.add(callBack); | ||
| } | ||
| else { | ||
| console.warn(`[ ${this.constructor.name} ][ ON ][ CALLBACK ][ ${name} ][ ALREADY_EXIST ]`); | ||
| } | ||
| } | ||
| else { | ||
| this.subscriptions.set(name, new Set([callBack])); | ||
| } | ||
| } | ||
| off(name, callBack) { | ||
| const listeners = this.subscriptions.get(name); | ||
| if (listeners) { | ||
| if (listeners.has(callBack)) { | ||
| listeners.delete(callBack); | ||
| } | ||
| else { | ||
| console.warn(`[ ${this.constructor.name} ][ OFF ][ CALLBACK ][ ${name} ][ NOT_EXIST ]`); | ||
| } | ||
| } | ||
| else { | ||
| console.error(`[ ${this.constructor.name} ][ OFF ][ SUBSCRIPTION ][ ${name} ][ NOT_EXIST ]`); | ||
| } | ||
| } | ||
| clear() { | ||
| this.subscriptions.forEach((list) => list.clear()); | ||
| this.subscriptions.clear(); | ||
| } | ||
| } | ||
| exports.EventEmitter = EventEmitter; |
| export { User } from "./essence/user/User"; | ||
| export { UserInsert } from "./essence/user/UserInsert"; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var User_1 = require("./essence/user/User"); | ||
| exports.User = User_1.User; | ||
| var UserInsert_1 = require("./essence/user/UserInsert"); | ||
| exports.UserInsert = UserInsert_1.UserInsert; |
| import { IUser } from "../../interfaces/essence/IUser"; | ||
| import { IPersist } from "../../interfaces/IPersist"; | ||
| import { UserInsert } from "./UserInsert"; | ||
| export declare class User extends UserInsert implements IUser, IPersist { | ||
| readonly id: string; | ||
| constructor(data?: any); | ||
| toJS(): { | ||
| [key: string]: any; | ||
| }; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const detectID_1 = require("../../utils/detectID"); | ||
| const UserInsert_1 = require("./UserInsert"); | ||
| class User extends UserInsert_1.UserInsert { | ||
| constructor(data) { | ||
| super(data); | ||
| this.id = detectID_1.detectID(data); | ||
| } | ||
| toJS() { | ||
| return Object.assign({}, super.toJS(), { id: this.id }); | ||
| } | ||
| } | ||
| exports.User = User; |
| import { IUser } from "../../interfaces/essence/IUser"; | ||
| export declare class UserInsert implements IUser { | ||
| readonly login: string; | ||
| readonly salt: string; | ||
| hashed_password: string; | ||
| constructor(data?: any); | ||
| toJS(): { | ||
| [key: string]: any; | ||
| }; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const isType_1 = require("../../utils/isType"); | ||
| class UserInsert { | ||
| constructor(data) { | ||
| this.login = ""; | ||
| this.salt = ""; | ||
| this.hashed_password = ""; | ||
| if (data) { | ||
| if (data.login && isType_1.isString(data.login)) { | ||
| this.login = data.login; | ||
| } | ||
| else { | ||
| throw new Error(`[${this.constructor.name}][ login ], must be a string;`); | ||
| } | ||
| if (data.salt && isType_1.isString(data.salt)) { | ||
| this.salt = data.salt; | ||
| } | ||
| if (data.hashed_password && isType_1.isString(data.hashed_password)) { | ||
| this.salt = data.salt; | ||
| } | ||
| } | ||
| } | ||
| toJS() { | ||
| return { | ||
| hashed_password: this.hashed_password, | ||
| login: this.login, | ||
| salt: this.salt, | ||
| }; | ||
| } | ||
| } | ||
| exports.UserInsert = UserInsert; |
| import { IInsert } from "../IInsert"; | ||
| export interface IUser extends IInsert { | ||
| login: string; | ||
| salt: string; | ||
| hashed_password: string; | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
| export declare function authenticate(password: string, salt: string, hashedPassword: string): boolean; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const encryptPassword_1 = require("./encryptPassword"); | ||
| function authenticate(password, salt, hashedPassword) { | ||
| return encryptPassword_1.encryptPassword(password, salt) === hashedPassword; | ||
| } | ||
| exports.authenticate = authenticate; |
| export declare function encryptPassword(password: string, salt: string): string; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const crypto = require("crypto"); | ||
| function encryptPassword(password, salt) { | ||
| try { | ||
| return crypto.createHmac("sha256", salt).update(password).digest("hex"); | ||
| } | ||
| catch (err) { | ||
| return ""; | ||
| } | ||
| } | ||
| exports.encryptPassword = encryptPassword; |
| export declare function getSalt(): string; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| function getSalt() { | ||
| return `${Math.round((new Date().valueOf() * Math.random()))}`; | ||
| } | ||
| exports.getSalt = getSalt; |
| export declare function uploadHTML(title: string, body: string, css: string[], fileName: string): void; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| function uploadHTML(title, body, css, fileName) { | ||
| const styles = css | ||
| .map((style) => (`<style type="text/css">${style}</style>`)) | ||
| .join("\n\r"); | ||
| const html = ` | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>${title}</title> | ||
| ${styles} | ||
| </head> | ||
| <body>${body}</body> | ||
| </html> | ||
| `; | ||
| const blob = new Blob([html], { type: "text/html" }); | ||
| const a = document.createElement("a"); | ||
| const url = URL.createObjectURL(blob); | ||
| a.href = url; | ||
| a.download = `${fileName}.html`; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| document.body.removeChild(a); | ||
| window.URL.revokeObjectURL(url); | ||
| } | ||
| exports.uploadHTML = uploadHTML; |
+3
-0
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var toMongo_1 = require("./back/toMongo"); | ||
| exports.toMongo = toMongo_1.toMongo; |
| import { ObjectId } from "bson"; | ||
| import { IInsert } from "../interfaces/IInsert"; | ||
| declare function toMongo<T extends IInsert>(instance: T): { | ||
| export declare function toMongo<T extends IInsert>(instance: T): { | ||
| [key: string]: any; | ||
@@ -8,2 +8,1 @@ } & { | ||
| }; | ||
| export default toMongo; |
@@ -11,4 +11,4 @@ "use strict"; | ||
| } | ||
| return Object.assign({}, { _id: bson_1.ObjectId(id) }, data); | ||
| return Object.assign({}, { _id: new bson_1.ObjectId(id) }, data); | ||
| } | ||
| exports.default = toMongo; | ||
| exports.toMongo = toMongo; |
| export { IPersist } from "./interfaces/IPersist"; | ||
| export { IInsert } from "./interfaces/IInsert"; | ||
| export { IUser } from "./interfaces/essence/IUser"; |
+5
-0
@@ -5,2 +5,3 @@ export { isString } from "./utils/isType"; | ||
| export { isUndefined } from "./utils/isType"; | ||
| export { isObject } from "./utils/isType"; | ||
| export { isFunction } from "./utils/isType"; | ||
@@ -15,1 +16,5 @@ export { isDate } from "./utils/isType"; | ||
| export { timeOnly } from "./utils/timeOnly"; | ||
| export { authenticate } from "./utils/authenticate"; | ||
| export { getSalt } from "./utils/getSalt"; | ||
| export { encryptPassword } from "./utils/encryptPassword"; | ||
| export { uploadHTML } from "./utils/uploadHTML"; |
+13
-3
@@ -12,7 +12,9 @@ "use strict"; | ||
| var isType_5 = require("./utils/isType"); | ||
| exports.isFunction = isType_5.isFunction; | ||
| exports.isObject = isType_5.isObject; | ||
| var isType_6 = require("./utils/isType"); | ||
| exports.isDate = isType_6.isDate; | ||
| exports.isFunction = isType_6.isFunction; | ||
| var isType_7 = require("./utils/isType"); | ||
| exports.isArray = isType_7.isArray; | ||
| exports.isDate = isType_7.isDate; | ||
| var isType_8 = require("./utils/isType"); | ||
| exports.isArray = isType_8.isArray; | ||
| var dateOnly_1 = require("./utils/dateOnly"); | ||
@@ -30,1 +32,9 @@ exports.dateOnly = dateOnly_1.dateOnly; | ||
| exports.timeOnly = timeOnly_1.timeOnly; | ||
| var authenticate_1 = require("./utils/authenticate"); | ||
| exports.authenticate = authenticate_1.authenticate; | ||
| var getSalt_1 = require("./utils/getSalt"); | ||
| exports.getSalt = getSalt_1.getSalt; | ||
| var encryptPassword_1 = require("./utils/encryptPassword"); | ||
| exports.encryptPassword = encryptPassword_1.encryptPassword; | ||
| var uploadHTML_1 = require("./utils/uploadHTML"); | ||
| exports.uploadHTML = uploadHTML_1.uploadHTML; |
@@ -1,1 +0,1 @@ | ||
| export declare const detectID: (data?: any) => string; | ||
| export declare function detectID(data?: any): string; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const isType_1 = require("../utils/isType"); | ||
| exports.detectID = (data) => { | ||
| function detectID(data) { | ||
| if (data) { | ||
@@ -20,2 +20,3 @@ if (isType_1.isString(data.id) && data.id.length > 0) { | ||
| return ""; | ||
| }; | ||
| } | ||
| exports.detectID = detectID; |
+4
-4
| { | ||
| "name": "@borodindmitriy/base-code", | ||
| "description": "Common classes's library.", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "engines": { | ||
@@ -41,6 +41,6 @@ "node": ">=8", | ||
| "devDependencies": { | ||
| "@types/bson": "1.0.8", | ||
| "@types/chai": "4.1.2", | ||
| "@types/mocha": "5.2.0", | ||
| "@types/node": "9.6.6", | ||
| "@types/twix": "0.6.37", | ||
| "bson": "2.0.5", | ||
@@ -58,3 +58,2 @@ "chai": "4.1.2", | ||
| "tslint-no-circular-imports": "0.3.0", | ||
| "twix": "1.2.1", | ||
| "typescript": "2.8.3" | ||
@@ -68,3 +67,4 @@ }, | ||
| }, | ||
| "license": "MIT" | ||
| "license": "MIT", | ||
| "dependencies": {} | ||
| } |
17621
88.72%17
-5.56%50
66.67%439
125.13%