Installations
npm install @lazy-toolbox/portable
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
16.15.1
NPM Version
8.11.0
Score
75.4
Supply Chain
99
Quality
76.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (87.31%)
JavaScript (12.16%)
HTML (0.53%)
Developer
FriquetLuca
Download Statistics
Total Downloads
2,855
Last Day
3
Last Week
13
Last Month
178
Last Year
505
GitHub Statistics
71 Commits
1 Watching
2 Branches
2 Contributors
Bundle Size
15.17 kB
Minified
4.51 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.0.18
Package Id
@lazy-toolbox/portable@0.0.18
Unpacked Size
120.52 kB
Size
26.48 kB
File Count
43
NPM Version
8.11.0
Node Version
16.15.1
Publised On
25 Jan 2023
Total Downloads
Cumulative downloads
Total Downloads
2,855
Last day
0%
3
Compared to previous day
Last week
-90.4%
13
Compared to previous week
Last month
612%
178
Compared to previous month
Last year
-37.4%
505
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
Lazy Toolbox - Portable
A NodeJS toolbox made for a lazy development anywhere you need.
Made to handle a bunch of cases that have to be handle on either a server or a client part.
The source code is available on GitHub.
There is also a bundle for thoses who don't want to use NodeJS. To use it, just add in your HTML:
1<script src="./lazyPortable.js" type="module"></script>
You can change the type module if needed.
Suppose you have a test.js
script in which you want to use the lazyPortable.js
bundle like this for example:
1<script src="./test.js" defer></script>
You can use the LazyPortable
global variable to access all of the LazyPortable
classes.
Example:
1console.log(LazyPortable.LazyMath.modulo(3, 2));
Index
Installation (NPM)
The installation is pretty straight forward:
1npm i @lazy-toolbox/portable
Updates
v0.0.18 - Bundle and Singleton
New content was added:
- Add
LazySingleton
class. - A bundle for portability.
v0.0.17 - Topological sort
New content was added:
- Add
LazyCounter
class. - Add
LazySort
class.
v0.0.13 - New rules
New content was added:
- Add
simpleKeys
toLazyRule
. - Add
parseString
toLazyRule
. - Add
regex
toLazyRule
.
New modifications were introduced:
- Introduction of an override of
patternSet
andIsPatternEnd
insimpleCharbox
for new rules in nested content. - Add
exp
parse for numbers. - Patch the wrong return type of
combinationArrayNRNO
inLazyMath
.
v0.0.10 - Charbox failing
New modifications were introduced:
- Correction of
simpleCharbox
fromLazyRule
not using it's end pattern and the spacing. - Patch
LazyParser
spacing.
v0.0.5 - Parsing rules
New content was added:
- Add
variable
,keyword
andany
static functions toLazyRule
. - Add
toStringDebug
static function toLazyParsing
.
New modifications were introduced:
- Change the
parse
return value to aPatternFound[]
. Previously, it wasany[]
.
v0.0.3 - Parsing fury
New content was added:
- Add
LazyParsing
class. - Add
LazyPattern
class. - Add
LazyRule
class. - Add
LazyText
class.
v0.0.2 - Lazy Mapping
New content was added:
- Add
LazyMapper
class for data filtering. - Add
LazyDataGraph
class for tangential analysis of graphs. - Add
combinationArrayNRNO
function toLazyMath
.
v0.0.0 - Initial commit
Documentation
This part explain all tools with examples if it's needed.
Portable
dateLog
1function dateLog(msg: any): string
Create a message with the time display up to the s.
It will be showned as [HH:MM:SS] MY_MESSAGE
.
Example:
1const { dateLog } = require('@lazy-toolbox/portable'); 2console.log(dateLog("Hello world")); // [10:37:12] Hello world
dateLogMS
1function dateLogMS(msg: any): string
Create a message with the time display up to the ms.
It will be showned as [HH:MM:SS.DCM] MY_MESSAGE
.
Example:
1const { dateLogMS } = require('@lazy-toolbox/portable'); 2console.log(dateLogMS("Hello world")); // [10:37:12.123] Hello world
getType
1function getType(parameter: any): string
Get the type of the parameter, extending typeof
to support class
and array
as native options.
Example:
1const { getType } = require('@lazy-toolbox/portable'); 2class Animal { 3 constructor(name) { 4 this.name = name; 5 } 6} 7const x = Animal; 8const y = [ 'a', 'b' ]; 9console.log(getType(x)); // class 10console.log(getType(y)); // array 11// Everything else is the same as typeof
LazyCounter
1interface RequiredMaterial { 2 name: string; 3 quantity?: number; 4 price?: number; 5} 6interface MaterialCounter { 7 name: string; 8 required?: RequiredMaterial[]; 9 price: number; 10} 11class LazyCounter { 12 static fullPrice(itemName: string, ...materials: MaterialCounter[]): number; 13 static allRowMaterials(itemName: string, ...materials: MaterialCounter[]): RequiredMaterial[]; 14}
A lazy way to count in crafting structure.
Example:
1const { LazyCounter } = require('@lazy-toolbox/portable'); 2const materials = [ 3 { 4 name: "wood", 5 price: 1 6 }, 7 { 8 name: "steel", 9 price: 5 10 }, 11 { 12 name: "sword", 13 required: [ 14 { 15 name: "wood", 16 quantity: 1 17 }, 18 { 19 name: "steel", 20 quantity: 2 21 } 22 ], 23 price: 100 24 } 25] 26console.log(LazyCounter.fullPrice("sword", materials)); // 111 27for(const item of LazyCounter.allRowMaterials("sword", materials)) { 28 console.log(`${item.name}: ${item.quantity}`); 29} 30/* 31wood: 1 32steel: 2 33*/
LazyDataGraph
1interface GraphPoint { 2 value: number; 3 label: string; 4 increasePercent?: number; 5 localMean?: number; 6 localVariance?: number; 7} 8class LazyDataGraph { 9 constructor(...datas: GraphPoint[]); 10 get points(): GraphPoint[]; 11 set points(pts: GraphPoint[]); 12 isTangentGraph(): boolean; 13 getTangentGraph(): LazyDataGraph; 14 generateSlope(): GraphPoint[]; 15}
A non-visual graph to analyze variation in datas.
Example:
1const { LazyDataGraph } = require('@lazy-toolbox/portable'); 2// Create the graph 3const lazyGraph = new LazyDataGraph( 4 // Set an ordered bunch of points 5 {label:'d1', value:100}, 6 {label:'d2', value:100}, 7 {label:'d3', value:200}, 8 {label:'d4', value:150}, 9 {label:'d5', value:100} 10); 11// Generate the tangent of the graph to see the differentiation in the graph 12const tangentGraph = lazyGraph.generateSlope(); 13// Just showing what was made on the way. 14for(let tanPt of tangentGraph) { 15 console.log(`- ${tanPt.label}: [value: ${tanPt.value}, increasePercent: ${tanPt.increasePercent}, localMean: ${tanPt.localMean}, localVariance: ${tanPt.localVariance}]`); 16} 17/* Result: 18- d1-d2: [value: 0, increasePercent: 0.0 ] 19- d2-d3: [value: 100, increasePercent: 2.0 ] 20- d3-d4: [value: -50, increasePercent: -0.25 ] 21- d4-d5: [value: -50, increasePercent: -0.33 ] 22*/
LazyMapper
1class LazyMapper { 2 static filterData<T>(data: any, defaultValue: T, transform: (d: any) => T, filter: (d: T) => T): T; 3 static defaultData<T>(data: any, defaultValue: T, transform: (d: any) => T): T; 4 static boolean(data: any): boolean; 5 static defaultBoolean(data: any, defaultValue: boolean): boolean; 6 static number(data: any): number; 7 static defaultNumber(data: any, defaultValue: number): number; 8 static filterNumber(data: any, defaultValue: number, filter: (d: number) => number): number; 9 static string(data: any): string; 10 static defaultString(data: any, defaultValue: string): string; 11 static filterString(data: any, defaultValue: string, filter: (d: string) => string): string; 12}
A mapper to allow some filtering for retrieved variables that could be undefined.
Example:
1const { LazyMapper } = require('@lazy-toolbox/portable');
2const someData = {
3 propA: "hello",
4 propB: 123,
5 propC: {
6 subProp: "uwu"
7 }
8};
9console.log(LazyMapper.defaultString(someData.propA, 'error!')); // hello
10console.log(LazyMapper.defaultString(someData.propD, 'error!')); // error!
LazyMath
1class LazyMath { 2 static modulo(a: number, b: number): number; 3 static frac(a: number): number; 4 static saturate(a: number): number; 5 static sum(k: number, n: number, f: (i: number) => number): number; 6 static product(k: number, n: number, f: (i: number) => number): number; 7 static isPrime(n: number): boolean; 8 static step(n: number, x: number): number; 9 static lerp(a: number, b: number, t: number): number; 10 static unlerp(a: number, b: number, p: number): number; 11 static binomialCoefficient(n: number, k: number): number; 12 static derivative(x: number, f: (x: number) => number): number; 13 static antiDerivative(x: number, f: (x: number) => number, subdivide: number = 1): number; 14 static integral(a: number, b: number, f: (x: number) => number, subdivide: number = 1): number; 15 static combinationArrayNRNO<T>(objects: T[], k: number): T[]; 16}
Add some lazy math that should have been available at first on JS.
Example:
1const { LazyMath } = require('@lazy-toolbox/portable');
2// The JS modulo operator violate the property (a + n) mod n = a mod n.
3// So we've implemented a modulo that doesn't violate it.
4// JS modulo = a - ([a / b] * b)
5// where [a / b] is the truncature of a / b.
6// LazyMath.modulo = a - (⌊a / b⌋ * b)
7// where ⌊a / b⌋ is the floor of a / b.
8
9// Positive value have the same answer
10console.log(LazyMath.modulo(4, 3)); // 1
11console.log(4 % 3) // 1
12// The JS modulo problem lies over here.
13console.log(LazyMath.modulo(-4, 3)); // 2
14console.log(-4 % 3); // -1
15
16// Get the leftover to obtain an integer less or equal to n.
17console.log(LazyMath.frac(2.345)); // 0.345
18console.log(LazyMath.frac(-2.345)); // 0.655
19
20// Get a value between 0 and 1
21console.log(LazyMath.saturate(2.345)); // 1
22
23// sum and product are made to handle iterative function for sum and product.
24// 1 + 2 + 3 + 4 = 10
25console.log(LazyMath.sum(1, 4, (i) => i));
26// 1 * 2 * 3 * 4 * 5 = 5! = 120
27console.log(LazyMath.product(1, 5, (i) => i));
28
29// A method to test if a number is prime.
30// It's not an optimal method, it can be slow as hell but you'll be 100% sure it's a prime number.
31console.log(LazyMath.isPrime(7)); // True
32console.log(LazyMath.isPrime(24)); // False
33
34// Return 1 if x is gequal to n, otherwise n.
35console.log(LazyMath.step(0.3, 0.5)); // 0.3
36console.log(LazyMath.step(0.4, 0.5)); // 0.4
37console.log(LazyMath.step(0.5, 0.5)); // 1
38
39// Do a linear interpolation between a and b using the parameter t for the interpolated distance.
40console.log(LazyMath.lerp(1, 3, 0.5)); // 2
41
42// Get the interpolated distance of p on the line from a to b.
43console.log(LazyMath.unlerp(1, 3, 2)); // 0.5
44
45// Compute the number of ways to choose an unordered subset of k elements from a fixed set of n elements.
46console.log(LazyMath.binomialCoefficient(5, 2)); // 10
47
48// Evaluate the derivative of a function f at a point x. d/dx f(x)
49// For this example, we use the function f(x) = x² and evaluate it's derivative at x = 3.
50// The result should be 6 if the approximation was perfect.
51console.log(LazyMath.derivative(3, (x) => { return x * x; })); // 5.921189464667501
52
53// Evaluate the anti-derivative of a function f' at a point x.
54// For this example, we use the function f'(x) = 2x and evaluate it's anti derivative at x = 3.
55// The result should be 9 if the approximation was perfect.
56console.log(LazyMath.antiDerivative(3, (x) => { return 2 * x; })); // 8.819999999999999
57
58// Evaluate the area under the curve of a function f' from a to b.
59// The result should be 15 if the approximation was perfect.
60console.log(LazyMath.integral(1, 4, (x) => { return 2 * x; })); // 14.819999999999997
61
62// Return an array of ordered combination without repetition of n objets (a string array) classified in k groups.
63console.log(LazyMath.combinationArrayNRNO([7, 6, 3, 4], 2));
64/* Result:
65[
66 [7, 6],
67 [7, 3],
68 [7, 4],
69 [6, 3],
70 [6, 4],
71 [3, 4]
72]
73*/
LazyParsing
1interface PatternResult {
2 isPatternEnd: boolean;
3 result: PatternFound[];
4 lastIndex: number;
5}
6class LazyParsing {
7 constructor(...rules: BasicRule[]);
8 addRules(...rules: BasicRule[]): void;
9 removeRules(...rulesName: string[]): void;
10 parse(text: string): PatternFound[];
11 static createSet(...rules: BasicRule[]): LazyPattern[];
12 static parse(txtContent: string, patternSet: LazyPattern[], i: number = 0, endPattern: (i: number, c: string, t: string) => boolean = (i: number, c: string, t: string) => { return false; }): PatternResult;
13 static toString(content: PatternResult | PatternFound[], spacing: boolean = false): string;
14 static toStringDebug(content: PatternResult | PatternFound[], spacing: boolean = false): string;
15}
A more natural way to parse datas with custom rules set in specific testing order.
Example:
1const { LazyParsing, LazyRule } = require('@lazy-toolbox/portable');
2// Create some keywords
3const keywordList = [
4 "if",
5 "as"
6];
7// Create a bunch of rules for the parser
8const ruleSet = LazyParsing.createSet(
9 LazyRule.keyword(keywordList), // Should look first for keywords
10 // If not a keyword, check for a variable
11 LazyRule.variable(), // This order is to make sure we don't treat a keyword as variable
12 LazyRule.number() // Last case scenario for the parsing is to check for a number
13);
14// Create a string to parse
15const contentToParse = "select a content as aswell 100 _times if needed!";
16// Get the parsing result
17const parsedResult = LazyParsing.parse(contentToParse, ruleSet);
18// Debug your datas visually
19console.log(LazyParsing.toStringDebug(parsedResult, true));
20/* Result:
21 [variable]: select
22 [variable]: a
23 [variable]: content
24 [keyword]: as
25 [variable]: aswell
26 [number]: 100
27 [variable]: _times
28 [keyword]: if
29 [variable]: needed
30*/
31// Everything after this is up to you, it's your datas, handle them the way you want to.
LazyPattern
1interface PatternFound {
2 name?: string,
3 currentName?: string,
4 begin?: string,
5 end?: string,
6 nested?: boolean,
7 content?: any,
8 error?: boolean,
9 line?: number,
10 lineChar?: number,
11 lastIndex?: number
12}
13class LazyPattern {
14 constructor(pattern: BasicRule);
15 get name(): string;
16 isActualPattern(i: number, c: string, t: string): boolean;
17 isEndPattern(i: number, c: string, t: string): boolean;
18 fetchContent(i: number, c: string, t: string, patternSet: LazyPattern[], actualPattern: LazyPattern): PatternFound;
19}
LazyPattern is a generic class made to check for pattern while looking inside a string. It fetch it's inner value with the pattern founded and then return it's last index.
LazyRule
1interface BasicRule { 2 name?: string, 3 defaultValue?: any, 4 begin?: string, 5 end?: string, 6 isPattern: (i: number, c: string, t: string) => boolean, 7 isPatternEnd?: (i: number, c: string, t: string) => boolean, 8 fetch?: (i: number, c: string, t: string, isPatternEnd?: (i: number, c: string, t: string) => boolean, patternSet?: LazyPattern[]) => PatternFound 9} 10class LazyRule { 11 static simpleChar(name: string, predicate: (c:string)=>boolean): BasicRule; 12 static simpleKeys(name: string, ...extractStrings: string[]): BasicRule; 13 static simpleCharbox(name: string, begin: string, end: string, overridePatternSet?: LazyPattern[], overrideIsPatternEnd?: (i: number, c: string, txt: string) => boolean): BasicRule; 14 static word(): BasicRule; 15 static number(comaOverDot: boolean = false, exp: boolean = false): BasicRule; 16 static variable(): BasicRule; 17 static keyword(...keywordList: string[]): BasicRule; 18 static any(name: string): BasicRule; 19 static parseString(name: string, between: string): BasicRule; 20 static regex(name: string, regex: RegExp): BasicRule; 21}
A generic rule maker. It creates rules for LazyParsing.
Example:
1const { LazyParsing, LazyRule } = require('@lazy-toolbox/portable');
2const parsingRules = LazyParsing.createSet(LazyRule.number(), LazyRule.word());
LazySingleton
1class LazySingleton { 2 protected constructor(); 3 static instanceFactory<T extends LazySingleton>(this: new (...args: any[]) => T, ...args: any[]): T; 4 static getInstance<T extends LazySingleton>(): T; 5}
A lazy singleton representation to not bother about doing it at all nor ever.
Example:
1const { LazySingleton } = require('@lazy-toolbox/portable'); 2class ExampleSingleton extends LazySingleton { 3 constructor(name) { 4 super(); 5 this.name = name; 6 } 7 sayName() { 8 return `My name is ${this.name}`; 9 } 10} 11const myExampleSingleton = new ExampleSingleton.instanceFactory("Amazing"); 12console.log(myExampleSingleton.sayName(myExampleSingleton));
LazySort
1interface RequiredOrder { 2 name: string, 3 content: any, 4 required?: string[] 5} 6class LazySort { 7 static byRequired(myDatas: RequiredOrder[], allMustExist: boolean = false): RequiredOrder[]; 8}
A lazy way to sort some particular structure.
Example:
1const { LazySort } = require('@lazy-toolbox/portable'); 2const testDatas = [ 3 { 4 name: "Cart", 5 content: "Cart making", 6 required: [ 7 "Fire", 8 "Wheel", 9 "Iron", 10 ] 11 }, 12 { 13 name: "Minerals", 14 content: "Minerals extraction" 15 }, 16 { 17 name: "Wheel", 18 content: "Wheel discovery" 19 }, 20 { 21 name: "Car", 22 content: "Car making", 23 required: [ 24 "Engine", 25 "Cart", 26 "Wheel" 27 ] 28 }, 29 { 30 name: "Fire", 31 content: "Fire discovery" 32 }, 33 { 34 name: "Iron", 35 content: "Iron discovery", 36 required: [ 37 "Fire", 38 "Minerals" 39 ] 40 } 41]; 42const showContent = (label, ds) => { 43 console.log(label); 44 let i = 1; 45 for(const d of ds) { 46 console.log(`${i++}) ${d.name}`); 47 } 48} 49showContent("Not all must exist", LazySort.byRequired(testDatas, false)); 50showContent("All must exist", LazySort.byRequired(testDatas, true)); 51/* 52Not all must exist 531) Fire 542) Wheel 553) Minerals 564) Iron 575) Cart 586) Car 59All must exist 601) Fire 612) Wheel 623) Minerals 634) Iron 645) Cart 65*/
LazyText
1class LazyText { 2 static extract(content: string, index: number, nbrLetters: number): string; 3 static extractFromUntil(content: string, startIndex: number, predicate: (c: string, i: number, txt: string)=>boolean): { value: string; lastIndex: number; }; 4 static countLines(content: string): number; 5 static countLinesChar(content: string, maxIndex: number): { lines: number; lineChar: number; }; 6}
Shorthand static class for special string functions.
Example:
1const { LazyText } = require('@lazy-toolbox/portable');
2const someContent = "Hello World.\nNice to meet you all.";
3console.log(LazyText.extract(someContent, 2, 3)); // "llo"
4console.log(LazyText.extractFromUntil(someContent, 2, (c, i, txt) => {
5 c === '/n'
6})); // "llo World."
7console.log(LazyText.countLines(someContent));// 2
8console.log(LazyText.countLinesChar(someContent));
9/*
10{
11 lines: 2
12 lineChar: 21
13}
14*/
No vulnerabilities found.
No security vulnerabilities found.
Gathering detailed insights and metrics for @lazy-toolbox/portable