Skip to content

Commit e92825e

Browse files
author
xnacly
authored
Merge pull request #2 from Flam3rboy/main
t
2 parents dceb965 + 4c28b8f commit e92825e

39 files changed

+1722
-866
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
mnJSf that should be the base lib for every JS project whether for browser or nodejs
44

5-
This library extends the properties of `Array`, `Object`, `Promise`, `Global` and `String`
5+
This library extends the properties of `Array`, `Object`, `Promise`, `Global`, `Math`, `Number` and `String`
66

77
## Installation
88

@@ -103,6 +103,23 @@ Global {
103103
}
104104
```
105105

106+
### [Number](/dist/Number.d.ts)
107+
108+
```ts
109+
Number {
110+
toInt(): number; // converts the current number to an integer (remove the numbers after the dot)
111+
}
112+
```
113+
114+
### [Math](/dist/Math.d.ts)
115+
116+
```ts
117+
Math {
118+
static randomBetween(min: number, max: number): number; // generates a random floating point number between min and max
119+
static randomIntBetween(min: number, max: number): number; // generates a random integer between min and max
120+
}
121+
```
122+
106123
## Example
107124

108125
### Array
@@ -262,3 +279,20 @@ console.log(convert, btoa(convert));
262279
console.log(atob(converted), converted);
263280
// -> this string was base64 encoded dGhpcyBzdHJpbmcgd2FzIGJhc2U2NCBlbmNvZGVk
264281
```
282+
283+
### Number
284+
285+
```js
286+
const x = 3.14;
287+
console.log(`Floating point number ${x} to int: `, x.toInt());
288+
// -> 3
289+
```
290+
291+
### Math
292+
```js
293+
console.log("Random number between 1-10", Math.randomBetween(1, 10));
294+
// -> 8.54098
295+
296+
console.log("Random int between 1-10", Math.randomIntBetween(1, 10));
297+
// -> 5
298+
```

dist/Array.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare global {
1010
random(): T | undefined;
1111
unique(): T[];
1212
shuffle(): T[];
13-
insert(elem: T, index: number): T[];
13+
insert(elem: T, index: number): this;
1414
count(search: RegExp | any): number;
1515
similarities(arr: T[]): T[];
1616
missing(arr: T[]): T[];

dist/Array.js

Lines changed: 98 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -20,177 +20,104 @@ var __spread = (this && this.__spread) || function () {
2020
return ar;
2121
};
2222
Object.defineProperty(exports, "__esModule", { value: true });
23-
try {
24-
Object.defineProperties(Array.prototype, {
25-
remove: {
26-
enumerable: false,
27-
writable: true,
28-
configurable: true,
29-
value: function (elem) {
30-
// do not use filter to modify current array
31-
var index = this.indexOf(elem);
32-
if (index === -1)
33-
return this;
34-
this.splice(index, 1);
35-
return this; //.filter((e) => e !== elem);
36-
},
37-
},
38-
insert: {
39-
enumerable: false,
40-
writable: true,
41-
configurable: true,
42-
value: function (elem, index) {
43-
if (!index)
44-
index = this.length;
45-
return this.splice(index, 0, elem);
46-
},
47-
},
48-
flat: {
49-
enumerable: false,
50-
writable: true,
51-
configurable: true,
52-
value: function (depth) {
53-
if (depth === void 0) { depth = 1; }
54-
return this.reduce(function (acc, val) {
55-
return (Array.isArray(val) && depth >= 1) || depth === -1
56-
? acc.concat(val.flat(depth--))
57-
: acc.concat(val);
58-
}, []);
59-
},
60-
},
61-
last: {
62-
enumerable: false,
63-
writable: true,
64-
configurable: true,
65-
value: function () {
66-
return this[this.length - 1];
67-
},
68-
},
69-
first: {
70-
enumerable: false,
71-
writable: true,
72-
configurable: true,
73-
value: function () {
74-
return this[0];
75-
},
76-
},
77-
unique: {
78-
enumerable: false,
79-
writable: true,
80-
configurable: true,
81-
value: function () {
82-
return __spread(new Set(this));
83-
},
84-
},
85-
random: {
86-
enumerable: false,
87-
writable: true,
88-
configurable: true,
89-
value: function () {
90-
return this[Math.floor(Math.random() * this.length)];
91-
},
92-
},
93-
shuffle: {
94-
enumerable: false,
95-
writable: true,
96-
configurable: true,
97-
value: function () {
98-
var _a;
99-
for (var i = this.length - 1; i > 0; i--) {
100-
var j = Math.floor(Math.random() * (i + 1));
101-
_a = __read([this[j], this[i]], 2), this[i] = _a[0], this[j] = _a[1];
102-
}
103-
return this;
104-
},
105-
},
106-
findMap: {
107-
enumerable: false,
108-
writable: true,
109-
configurable: true,
110-
value: function (predicate) {
111-
for (var i = 0; i < this.length; i++) {
112-
var result = predicate(this[i], i, this);
113-
if (result) {
114-
return result;
115-
}
23+
var Util_1 = require("./Util");
24+
Util_1.define(Array.prototype, {
25+
remove: function (elem) {
26+
// do not use filter to modify current array
27+
var index = this.indexOf(elem);
28+
if (index === -1)
29+
return this;
30+
this.splice(index, 1);
31+
return this; //.filter((e) => e !== elem);
32+
},
33+
insert: function (elem, index) {
34+
if (!index)
35+
index = this.length;
36+
this.splice(index, 0, elem);
37+
return this;
38+
},
39+
flat: function (depth) {
40+
if (depth === void 0) { depth = 1; }
41+
return this.reduce(function (acc, val) {
42+
return (Array.isArray(val) && depth >= 1) || depth === -1 ? acc.concat(val.flat(depth--)) : acc.concat(val);
43+
}, []);
44+
},
45+
last: function () {
46+
return this[this.length - 1];
47+
},
48+
first: function () {
49+
return this[0];
50+
},
51+
unique: function () {
52+
return __spread(new Set(this));
53+
},
54+
random: function () {
55+
return this[Math.floor(Math.random() * this.length)];
56+
},
57+
shuffle: function () {
58+
var _a;
59+
for (var i = this.length - 1; i > 0; i--) {
60+
var j = Math.floor(Math.random() * (i + 1));
61+
_a = __read([this[j], this[i]], 2), this[i] = _a[0], this[j] = _a[1];
62+
}
63+
return this;
64+
},
65+
findMap: function (predicate) {
66+
for (var i = 0; i < this.length; i++) {
67+
var result = predicate(this[i], i, this);
68+
if (result) {
69+
return result;
70+
}
71+
}
72+
},
73+
findLast: function (predicate) {
74+
for (var i = this.length; i >= 0; i--) {
75+
if (predicate(this[i], i, this))
76+
return this[i];
77+
}
78+
return null;
79+
},
80+
findLastIndex: function (predicate) {
81+
for (var i = this.length - 1; i >= 0; i--) {
82+
if (predicate(this[i], i, this))
83+
return i;
84+
}
85+
return -1;
86+
},
87+
count: function (search) {
88+
var nativeTypes = ["string", "number", "object", "array"];
89+
var count = 0;
90+
this.forEach(function (element) {
91+
if (element === search) {
92+
count++;
93+
return;
94+
}
95+
// check if searchparam is a native class (l: 99)
96+
if (typeof search == "function") {
97+
var className = search.name.toLowerCase();
98+
if (nativeTypes.includes(className) && typeof element === className) {
99+
count++;
100+
return;
116101
}
117-
},
118-
},
119-
findLast: {
120-
enumerable: false,
121-
writable: true,
122-
configurable: true,
123-
value: function (predicate) {
124-
for (var i = this.length; i >= 0; i--) {
125-
if (predicate(this[i], i, this))
126-
return this[i];
102+
else if (element instanceof search) {
103+
count++;
104+
return;
127105
}
128-
return null;
129-
},
130-
},
131-
findLastIndex: {
132-
enumerable: false,
133-
writable: true,
134-
configurable: true,
135-
value: function (predicate) {
136-
for (var i = this.length - 1; i >= 0; i--) {
137-
if (predicate(this[i], i, this))
138-
return i;
106+
}
107+
// if element of array is a string: allow regex patterns
108+
if (typeof element === "string") {
109+
if (element.match(search)) {
110+
count++;
111+
return;
139112
}
140-
return -1;
141-
},
142-
},
143-
count: {
144-
enumerable: false,
145-
writable: true,
146-
configurable: true,
147-
value: function (search) {
148-
var nativeTypes = ["string", "number", "object", "array"];
149-
var count = 0;
150-
this.forEach(function (element) {
151-
if (element === search) {
152-
count++;
153-
return;
154-
}
155-
// check if searchparam is a native class (l: 99)
156-
if (typeof search == "function") {
157-
var className = search.name.toLowerCase();
158-
if (nativeTypes.includes(className) && typeof element === className) {
159-
count++;
160-
return;
161-
}
162-
else if (element instanceof search) {
163-
count++;
164-
return;
165-
}
166-
}
167-
// if element of array is a string: allow regex patterns
168-
if (typeof element === "string") {
169-
if (element.match(search)) {
170-
count++;
171-
return;
172-
}
173-
}
174-
});
175-
return count;
176-
},
177-
},
178-
missing: {
179-
enumerable: false,
180-
writable: true,
181-
configurable: true,
182-
value: function (arr) {
183-
return this.filter(function (x) { return !arr.includes(x); });
184-
},
185-
},
186-
similarities: {
187-
enumerable: false,
188-
writable: true,
189-
configurable: true,
190-
value: function (arr) {
191-
return this.filter(function (x) { return arr.includes(x); });
192-
},
193-
},
194-
});
195-
}
196-
catch (error) { }
113+
}
114+
});
115+
return count;
116+
},
117+
missing: function (arr) {
118+
return this.filter(function (x) { return !arr.includes(x); });
119+
},
120+
similarities: function (arr) {
121+
return this.filter(function (x) { return arr.includes(x); });
122+
},
123+
});

dist/Global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference types="node" />
12
declare global {
23
/**
34
* Converts a Base64 encoded string
@@ -9,5 +10,6 @@ declare global {
910
* @param {string} data UTF-8 string that should get encoded
1011
*/
1112
function btoa(data: string): string;
13+
function setIntervalNow(callback: Function, milliseconds?: number, ...args: any[]): number | NodeJS.Timeout;
1214
}
1315
export {};

dist/Global.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
"use strict";
2+
var __read = (this && this.__read) || function (o, n) {
3+
var m = typeof Symbol === "function" && o[Symbol.iterator];
4+
if (!m) return o;
5+
var i = m.call(o), r, ar = [], e;
6+
try {
7+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8+
}
9+
catch (error) { e = { error: error }; }
10+
finally {
11+
try {
12+
if (r && !r.done && (m = i["return"])) m.call(i);
13+
}
14+
finally { if (e) throw e.error; }
15+
}
16+
return ar;
17+
};
18+
var __spread = (this && this.__spread) || function () {
19+
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
20+
return ar;
21+
};
222
Object.defineProperty(exports, "__esModule", { value: true });
323
// @ts-ignore
424
var Global;
@@ -12,3 +32,14 @@ if (!Global.atob)
1232
Global.atob = function (data) { return Buffer.from(data, "base64").toString("utf8"); };
1333
if (!Global.btoa)
1434
Global.btoa = function (data) { return Buffer.from(data.toString(), "utf8").toString("base64"); };
35+
if (!Global.setIntervalNow) {
36+
Global.setIntervalNow = function (callback, milliseconds) {
37+
var args = [];
38+
for (var _i = 2; _i < arguments.length; _i++) {
39+
args[_i - 2] = arguments[_i];
40+
}
41+
var func = callback.bind.apply(callback, __spread([this], args));
42+
func();
43+
return setInterval(func, milliseconds);
44+
};
45+
}

dist/JSON.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare const getCircularReplacer: (other: any) => (key: string, value: any) => any;
2+
declare const stringify: {
3+
(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
4+
(value: any, replacer?: (string | number)[], space?: string | number): string;
5+
};

0 commit comments

Comments
 (0)