@cardsgame/utils
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -6,4 +6,24 @@ /** | ||
export declare const mapCompose: (array: any[], ...functions: ((...args: any[]) => any)[]) => any[]; | ||
/** | ||
* Function for `array.sort()`. | ||
*/ | ||
export declare const sortAlphabetically: (a: string, b: string) => number; | ||
/** | ||
* Function for `array.sort()`. | ||
*/ | ||
export declare const sortAlphaNumerically: (a: string, b: string) => number; | ||
/** | ||
* Returns new array with items shuffled around. | ||
*/ | ||
export declare const shuffle: (array: any[]) => any[]; | ||
/** | ||
* | ||
* @param count | ||
*/ | ||
export declare const arrayWith: (count: number) => any[]; | ||
/** | ||
* Grabs you most common "propKey" in your collection of `T`, | ||
* filtering out items which match given optional condition. | ||
* @returns a tuple of [`propValue`, `count`] | ||
*/ | ||
export declare function pickMostCommonProp<T = any>(collection: T[], propKey: string, condition?: (T: any) => boolean): [any, number]; |
@@ -23,2 +23,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.pickMostCommonProp = exports.arrayWith = exports.shuffle = exports.sortAlphaNumerically = exports.sortAlphabetically = exports.mapCompose = void 0; | ||
/** | ||
@@ -45,5 +46,11 @@ * Runs every provided function on `array` using .map(), ignores every function which turns out to be `undefined` instead. | ||
}; | ||
/** | ||
* Function for `array.sort()`. | ||
*/ | ||
exports.sortAlphabetically = function (a, b) { | ||
return a < b ? -1 : a > b ? 1 : 0; | ||
}; | ||
/** | ||
* Function for `array.sort()`. | ||
*/ | ||
exports.sortAlphaNumerically = function (a, b) { | ||
@@ -57,3 +64,45 @@ var numA = parseInt(a) || false; | ||
}; | ||
/** | ||
* Returns new array with items shuffled around. | ||
*/ | ||
exports.shuffle = function (array) { | ||
var res = __spread(array); | ||
for (var i = res.length - 1; i > 0; i--) { | ||
var j = Math.floor(Math.random() * (i + 1)); | ||
var temp = res[i]; | ||
res[i] = res[j]; | ||
res[j] = temp; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* | ||
* @param count | ||
*/ | ||
exports.arrayWith = function (count) { return __spread(Array(count).keys()); }; | ||
/** | ||
* Grabs you most common "propKey" in your collection of `T`, | ||
* filtering out items which match given optional condition. | ||
* @returns a tuple of [`propValue`, `count`] | ||
*/ | ||
function pickMostCommonProp(collection, propKey, condition) { | ||
var map = new Map(); | ||
collection | ||
.filter(function (item) { return (condition ? condition(item) : true); }) | ||
.filter(function (item) { return propKey in item; }) | ||
.forEach(function (item) { | ||
map.has(item[propKey]) | ||
? map.set(item[propKey], map.get(item[propKey]) + 1) | ||
: map.set(item[propKey], 1); | ||
}); | ||
var mostCommonProp = [undefined, 0]; | ||
map.forEach(function (count, propKey) { | ||
if (count > mostCommonProp[1]) { | ||
mostCommonProp[0] = propKey; | ||
mostCommonProp[1] = count; | ||
} | ||
}); | ||
return mostCommonProp; | ||
} | ||
exports.pickMostCommonProp = pickMostCommonProp; | ||
//# sourceMappingURL=arrays.js.map |
@@ -5,4 +5,5 @@ export * from "./arrays"; | ||
export * from "./numbers"; | ||
export * from "./objects"; | ||
export * from "./random"; | ||
export * from "./strings"; | ||
export * from "./utils"; |
"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" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./arrays")); | ||
__export(require("./logs")); | ||
__export(require("./mapSchema")); | ||
__export(require("./numbers")); | ||
__export(require("./random")); | ||
__export(require("./strings")); | ||
__export(require("./utils")); | ||
__exportStar(require("./arrays"), exports); | ||
__exportStar(require("./logs"), exports); | ||
__exportStar(require("./mapSchema"), exports); | ||
__exportStar(require("./numbers"), exports); | ||
__exportStar(require("./objects"), exports); | ||
__exportStar(require("./random"), exports); | ||
__exportStar(require("./strings"), exports); | ||
__exportStar(require("./utils"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -30,10 +30,17 @@ import Chalk from "chalk"; | ||
groupCollapsed: (...any: any[]) => void; | ||
groupEnd: () => void; | ||
groupEnd: (...any: any[]) => void; | ||
} | ||
/** | ||
* Local logging utility | ||
* TODO: whoops, port it to server environment too! | ||
*/ | ||
declare type LogsOptions = { | ||
browserStyle?: string; | ||
serverStyle?: Chalk.Chalk; | ||
}; | ||
export declare class Logs { | ||
constructor(name: string, style: string, enabled?: boolean); | ||
private readonly enabled; | ||
constructor(name: string, enabled?: boolean, options?: LogsOptions); | ||
setupServerLogs(name: string, style: Chalk.Chalk): void; | ||
setupBrowserLogs(name: string, style: string): void; | ||
} | ||
export {}; |
166
lib/logs.js
@@ -26,2 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Logs = exports.logs = exports.LogLevels = exports.chalk = exports.IS_CHROME = void 0; | ||
var chalk_1 = __importDefault(require("chalk")); | ||
@@ -77,4 +78,5 @@ var utils_1 = require("./utils"); | ||
var syntaxHighlight = function (arg) { | ||
if (exports.IS_CHROME) | ||
if (exports.IS_CHROME) { | ||
return arg; | ||
} | ||
if (typeof arg === "string") { | ||
@@ -84,3 +86,3 @@ return exports.chalk.gray(arg); | ||
if (typeof arg === "number") { | ||
return exports.chalk.red.bold("" + arg); | ||
return exports.chalk.red.bold(arg.toString()); | ||
} | ||
@@ -96,5 +98,5 @@ if (typeof arg === "boolean") { | ||
}; | ||
var indentLevel = 0; | ||
function getIndent() { | ||
return Array(indentLevel).fill("│ ").join(""); | ||
var _indentLevel = 0; | ||
function _getIndent() { | ||
return Array(_indentLevel).fill("│ ").join(""); | ||
} | ||
@@ -121,3 +123,3 @@ if (isBrowser) { | ||
console.debug.apply(console, __spread([ | ||
getIndent(), | ||
_getIndent(), | ||
"\t" | ||
@@ -133,3 +135,3 @@ ], args.map(function (arg) { return exports.chalk.gray(arg); }))); | ||
console.log.apply(console, __spread([ | ||
getIndent(), | ||
_getIndent(), | ||
first + ":" | ||
@@ -139,3 +141,3 @@ ], args.map(syntaxHighlight))); | ||
else { | ||
console.log.call(console, exports.chalk.gray(getIndent() + first)); | ||
console.log.call(console, exports.chalk.gray(_getIndent() + first)); | ||
} | ||
@@ -149,3 +151,3 @@ }, | ||
console.info.apply(console, __spread([ | ||
getIndent() + exports.chalk.bgBlue.black(" " + first + " ") | ||
_getIndent() + exports.chalk.bgBlue.black(" " + first + " ") | ||
], args.map(syntaxHighlight))); | ||
@@ -159,3 +161,3 @@ }, | ||
console.warn.apply(console, __spread([ | ||
getIndent() + exports.chalk.bgYellow.black(" " + first + " ") | ||
_getIndent() + exports.chalk.bgYellow.black(" " + first + " ") | ||
], args.map(syntaxHighlight))); | ||
@@ -169,3 +171,3 @@ }, | ||
console.error.apply(console, __spread([ | ||
getIndent() + exports.chalk.bgRed(" " + first + " ") | ||
_getIndent() + exports.chalk.bgRed(" " + first + " ") | ||
], args.map(syntaxHighlight))); | ||
@@ -179,3 +181,3 @@ }, | ||
exports.logs.notice.apply(exports.logs, __spread(["\u250D\u2501" + first], args)); | ||
indentLevel++; | ||
_indentLevel++; | ||
}, | ||
@@ -188,3 +190,3 @@ groupCollapsed: function (first) { | ||
exports.logs.notice.apply(exports.logs, __spread(["\u250D\u2501" + first], args)); | ||
indentLevel++; | ||
_indentLevel++; | ||
}, | ||
@@ -197,3 +199,3 @@ groupEnd: function (first) { | ||
} | ||
indentLevel = Math.max(--indentLevel, 0); | ||
_indentLevel = Math.max(_indentLevel - 1, 0); | ||
exports.logs.notice.apply(exports.logs, __spread(["\u2515\u2501" + first], args)); | ||
@@ -275,12 +277,120 @@ }, | ||
} | ||
/** | ||
* Local logging utility | ||
* TODO: whoops, port it to server environment too! | ||
*/ | ||
var Logs = /** @class */ (function () { | ||
function Logs(name, style, enabled) { | ||
function Logs(name, enabled, options) { | ||
if (enabled === void 0) { enabled = false; } | ||
this.enabled = enabled; | ||
if (isBrowser) { | ||
this.setupBrowserLogs(name, options.browserStyle); | ||
} | ||
else { | ||
this.setupServerLogs(name, options.serverStyle); | ||
} | ||
} | ||
Logs.prototype.setupServerLogs = function (name, style) { | ||
var indentLevel = 0; | ||
var getIndent = function () { | ||
return Array(indentLevel).fill("│ ").join(""); | ||
}; | ||
this["error"] = | ||
logLevel < LogLevels.error && enabled | ||
logLevel < LogLevels.error && this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
console.error.apply(console, __spread([ | ||
style(getIndent() + exports.chalk.bgRed(" " + first + " ")) | ||
], args.map(syntaxHighlight))); | ||
}; | ||
this["warn"] = | ||
logLevel < LogLevels.warn && this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
console.warn.apply(console, __spread([ | ||
style(getIndent() + exports.chalk.bgYellow.black(" " + first + " ")) | ||
], args.map(syntaxHighlight))); | ||
}; | ||
this["info"] = | ||
logLevel < LogLevels.info && this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
console.info.apply(console, __spread([ | ||
style(getIndent() + exports.chalk.bgBlue.black(" " + first + " ")) | ||
], args.map(syntaxHighlight))); | ||
}; | ||
this["notice"] = | ||
logLevel < LogLevels.notice && this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
if (args.length > 0) { | ||
console.log.apply(console, __spread([ | ||
style(getIndent(), first + ":") | ||
], args.map(syntaxHighlight))); | ||
} | ||
else { | ||
console.log.call(console, style(exports.chalk.gray(getIndent() + first))); | ||
} | ||
}; | ||
var notice = this["notice"]; | ||
this["verbose"] = | ||
logLevel < LogLevels.verbose && this.enabled | ||
? utils_1.noop | ||
: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
console.debug.apply(console, __spread([ | ||
style(getIndent() + "\t") | ||
], args.map(function (arg) { return exports.chalk.gray(arg); }))); | ||
}; | ||
this["group"] = !this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
notice.apply(void 0, __spread(["\u250D\u2501" + first], args)); | ||
indentLevel++; | ||
}; | ||
this["groupCollapsed"] = !this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
notice.apply(void 0, __spread(["\u250D\u2501" + first], args)); | ||
indentLevel++; | ||
}; | ||
this["groupEnd"] = !this.enabled | ||
? utils_1.noop | ||
: function (first) { | ||
if (first === void 0) { first = "────────────"; } | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
indentLevel = Math.max(indentLevel - 1, 0); | ||
notice.apply(void 0, __spread(["\u2515\u2501" + first], args)); | ||
}; | ||
}; | ||
Logs.prototype.setupBrowserLogs = function (name, style) { | ||
this["error"] = | ||
logLevel < LogLevels.error && this.enabled | ||
? utils_1.noop | ||
: (function () { | ||
@@ -290,3 +400,3 @@ return Function.prototype.bind.call(console.error, console, "%c " + name + " ", style); | ||
this["warn"] = | ||
logLevel < LogLevels.warn && enabled | ||
logLevel < LogLevels.warn && this.enabled | ||
? utils_1.noop | ||
@@ -297,3 +407,3 @@ : (function () { | ||
this["info"] = | ||
logLevel < LogLevels.info && enabled | ||
logLevel < LogLevels.info && this.enabled | ||
? utils_1.noop | ||
@@ -304,3 +414,3 @@ : (function () { | ||
this["notice"] = | ||
logLevel < LogLevels.notice && enabled | ||
logLevel < LogLevels.notice && this.enabled | ||
? utils_1.noop | ||
@@ -311,3 +421,3 @@ : (function () { | ||
this["verbose"] = | ||
logLevel < LogLevels.verbose && enabled | ||
logLevel < LogLevels.verbose && this.enabled | ||
? utils_1.noop | ||
@@ -317,9 +427,9 @@ : (function () { | ||
})(); | ||
this["group"] = enabled | ||
this["group"] = !this.enabled | ||
? utils_1.noop | ||
: console.group.bind(console, "%c " + name + " ", style); | ||
this["groupCollapsed"] = enabled | ||
this["groupCollapsed"] = !this.enabled | ||
? utils_1.noop | ||
: console.groupCollapsed.bind(console, "%c " + name + " ", style); | ||
this["groupEnd"] = enabled | ||
this["groupEnd"] = !this.enabled | ||
? utils_1.noop | ||
@@ -329,3 +439,3 @@ : (function () { | ||
})(); | ||
} | ||
}; | ||
return Logs; | ||
@@ -332,0 +442,0 @@ }()); |
export declare const map2Array: <T>(map: { | ||
[key: string]: any; | ||
}) => T[]; | ||
export declare const mapGetIdx: (map: {}, entry: any) => number; | ||
export declare const mapAdd: (map: {}, entry: any) => number; | ||
export declare const mapCount: (map: {}) => number; | ||
export declare const mapRemoveIdx: (map: {}, idx: number) => boolean; | ||
export declare const mapRemoveEntry: (map: {}, entry: any) => boolean; | ||
export declare const mapGetIdx: (map: Record<string, any>, entry: unknown) => number; | ||
export declare const mapAdd: (map: Record<string, any>, entry: unknown) => number; | ||
export declare const mapCount: (map: Record<string, any>) => number; | ||
export declare const mapRemoveIdx: (map: Record<string, any>, idx: number) => boolean; | ||
export declare const mapRemoveEntry: (map: Record<string, any>, entry: unknown) => boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapRemoveEntry = exports.mapRemoveIdx = exports.mapCount = exports.mapAdd = exports.mapGetIdx = exports.map2Array = void 0; | ||
exports.map2Array = function (map) { | ||
@@ -11,4 +12,5 @@ return Object.keys(map) | ||
for (var i = 0; i < max; i++) { | ||
if (map[i] === entry) | ||
if (map[i] === entry) { | ||
return i; | ||
} | ||
} | ||
@@ -26,4 +28,5 @@ return -1; | ||
exports.mapRemoveIdx = function (map, idx) { | ||
if (typeof map[idx] === "undefined") | ||
if (typeof map[idx] === "undefined") { | ||
return false; | ||
} | ||
var max = Object.keys(map).length - 1; | ||
@@ -38,6 +41,7 @@ for (var i = idx + 1; i <= max; i++) { | ||
var idx = exports.mapGetIdx(map, entry); | ||
if (idx === -1) | ||
if (idx === -1) { | ||
return false; | ||
} | ||
return exports.mapRemoveIdx(map, idx); | ||
}; | ||
//# sourceMappingURL=mapSchema.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decimal = exports.px2cm = exports.cm2px = exports.deg2rad = exports.rad2deg = exports.wrap = exports.limit = void 0; | ||
exports.limit = function (val, min, max) { | ||
@@ -4,0 +5,0 @@ if (min === void 0) { min = 0; } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.randomInt = exports.randomFloat = void 0; | ||
/** | ||
@@ -4,0 +5,0 @@ * Random float number |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sentenceCase = exports.camelCase = exports.randomName = exports.trim = void 0; | ||
exports.trim = function (string, maxLength) { | ||
@@ -4,0 +5,0 @@ if (string === void 0) { string = ""; } |
@@ -0,1 +1,2 @@ | ||
/// <reference types="@cardsgame/types" /> | ||
/** | ||
@@ -11,3 +12,3 @@ * Returns first, *defined* value | ||
*/ | ||
export declare const compose: (value: any, ...functions: ((...args: any[]) => any)[]) => any; | ||
export declare const compose: (value: unknown, ...functions: ((...args: any[]) => any)[]) => any; | ||
/** | ||
@@ -29,2 +30,2 @@ * Executes function multiple times | ||
export declare const isObject: (thing: unknown) => boolean; | ||
export declare function applyMixins(derivedCtor: any, baseCtors: any[]): void; | ||
export declare function applyMixins(derivedCtor: AnyClass, baseCtors: any[]): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.applyMixins = exports.isObject = exports.timeout = exports.times = exports.compose = exports.noop = exports.def = void 0; | ||
/** | ||
@@ -4,0 +5,0 @@ * Returns first, *defined* value |
{ | ||
"name": "@cardsgame/utils", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "", | ||
"author": "Darek Greenly (https://darekgreenly.com)", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
@@ -19,6 +19,10 @@ "types": "lib/index.d.ts", | ||
"dependencies": { | ||
"@cardsgame/types": "^0.8.0", | ||
"@colyseus/schema": "^0.5.30", | ||
"chalk": "^3.0.0" | ||
}, | ||
"gitHead": "db08f5674a86f730a75faee37414299079fd8eaf" | ||
"devDependencies": { | ||
"@types/node": "^14.0.23" | ||
}, | ||
"gitHead": "a44fb0f1391342ef8d0ab9a8bac0648afa73ae2d" | ||
} |
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
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
56887
30
0
924
3
1
+ Added@cardsgame/types@^0.8.0
+ Added@cardsgame/types@0.8.1(transitive)