|
1 |
| - |
2 | 1 | [![npm version][npm-version-src]][npm-version-href]
|
3 | 2 | [![npm downloads][npm-downloads-src]][npm-downloads-href]
|
4 | 3 |
|
|
7 | 6 | Functions</h1>
|
8 | 7 | <p align="center">A zero-dependecy JavaScript utility library delivering <b>missing</b> native functions 💼</p>
|
9 | 8 |
|
10 |
| -## Why using mnjsf? |
| 9 | +## Why use mnjsf? |
11 | 10 |
|
12 |
| -mnjsf delivers some functions which are missing in JavaScript |
| 11 | +mnjsf delivers functions missing from JavaScript |
13 | 12 |
|
14 |
| -❯ 💼 **Zero-Dependency** Built without a dependency hell<br> |
| 13 | +❯ 💼 **Zero-Dependency** Built without a dependency hell<br> |
15 | 14 | ❯ 📐 **Lightweight** 2.5KB<br>
|
16 |
| -❯ 🌐 **NodeJS/Browser Support** use mnjsf in a browser or Node.js application <br> |
| 15 | +❯ 🌐 **NodeJS/Browser Support** use mnjsf in a browser or a Node.js application <br> |
17 | 16 | ❯ ✨ **Direct Call** of utility functions on variables (`[].last()`) <br>
|
18 | 17 |
|
19 | 18 | This library extends the properties of `Array`, `Object`, `Promise`, `Global`, `Math`, `Number` and `String`
|
20 | 19 |
|
21 | 20 | ## [Documentation](https://trantlabs.github.io/missing-native-js-functions/)
|
22 | 21 |
|
23 |
| - |
24 | 22 | ## Installation
|
25 | 23 |
|
26 | 24 | ```
|
@@ -48,264 +46,6 @@ use in Browser
|
48 | 46 | <script src="https://cdn.jsdelivr.net/npm/missing-native-js-functions/dist/mnjsf.min.js"></script>
|
49 | 47 | ```
|
50 | 48 |
|
51 |
| -## [Reference](/dist/index.d.ts) |
52 |
| - |
53 |
| -### [Array](/dist/Array.d.ts) |
54 |
| - |
55 |
| -```ts |
56 |
| -Array { |
57 |
| - remove(value: T): this; // removes the value of the array (Notice: it modifies the current array) |
58 |
| - flat(depth?: number): T; // returns a new flattened array e.g. [[1,2],[3,4]] -> [1,2,3,4] |
59 |
| - first(): T; // returns the first element of the array |
60 |
| - last(): T; // returns the last element of the array |
61 |
| - findMap(predicate: (value: T, index: number, obj: T[]) => unknown, map: (value: T) => any): any | undefined; // finds a value in the array and maps it |
62 |
| - random(): T; // returns a random array element |
63 |
| - unique(): T[]; // returns a new unique array with distinct values |
64 |
| - shuffle(): T[]; // shuffles the current array |
65 |
| - insert(elem: T, index: number): T[]; // insert an element at a specified index |
66 |
| - count(search: RegExp | any): number; // returns total of found items for specified search |
67 |
| - similarities(arr: T[]): T[]; // returns a new array with elements that are both in this array and in the comparison array |
68 |
| - missing(arr: T[]): T[]; // returns a new array with elements that are in this array, but are missing in the comparison array |
69 |
| -} |
70 |
| -``` |
71 |
| - |
72 |
| -### [Object](/dist/Object.d.ts) |
73 |
| - |
74 |
| -```ts |
75 |
| -Object { |
76 |
| - forEach(callback: (element: any, index?: string) => any): void; // callback is called for every element in object |
77 |
| - map(callback: (element: any, index?: string) => any): this; // callback is called for every element in object and the result is returned as a new object |
78 |
| - keys(): string[]; //returns keys of object itself |
79 |
| - entries(): Array<[string, any]>; // returns a nested array of key and corresponding value of object itself |
80 |
| - merge(obj: any): any; // returns a new object deeply merged with obj, the current will overwrite obj, if obj has the same property. Notice will not merge classes |
81 |
| - stringify(): string; // parses the object to an string (trows errors if invalid input) |
82 |
| -} |
83 |
| -``` |
84 |
| - |
85 |
| -### [String](/dist/String.d.ts) |
86 |
| - |
87 |
| -```ts |
88 |
| -String { |
89 |
| - capitalize(): string; // Returns a new string with the first character capitalized |
90 |
| - replaceAll(search: string, replace: string): string; // Replace all occurrences of search with replace |
91 |
| - similarity(compare: string): number; // Returns a value between 0 (different) and 1 (same) indicating how similar the string is to compare |
92 |
| - join(iterate: string[]): string; // Returns the array values seperated by the given divider as a string |
93 |
| - partition(separator: string): string[]; // Returns split array, but includes separators |
94 |
| - toNumber(): number; // converts string to number, if not a number returns NaN |
95 |
| - toBigInt(): number; // converts string to BigInt, if not a number returns NaN |
96 |
| - count(countString: RegExp | any): number; // returns total of found items for specified search; |
97 |
| - swapcase(): string;// Returns a swapped case string -> aLL CASES ARE SWAPPED |
98 |
| - title(): string; // converts the string into a title string -> This Is A Title String |
99 |
| - toObject(): object; // parses the string to an object (trows errors if invalid input) |
100 |
| -} |
101 |
| -``` |
102 |
| - |
103 |
| -### [Promise](/dist/Promise.d.ts) |
104 |
| - |
105 |
| -```ts |
106 |
| -Promise { |
107 |
| - caught(): this; // catch all errors in the console without the need to specify a function, similar like promise.catch(console.error) |
108 |
| -} |
109 |
| -``` |
110 |
| - |
111 |
| -### [Global](/dist/Global.d.ts) |
112 |
| - |
113 |
| -```ts |
114 |
| -Global { |
115 |
| - function atob(data: string): string; // Converts a Base64 encoded string back to UTF-8 |
116 |
| - function btoa(data: string): string; // Converts a UTF-8 string to a Base64 encoded string |
117 |
| - function sleep(milliseconds: number): Promise<void>; // Returns a promise that resolves after the specified milliseconds |
118 |
| -} |
119 |
| -``` |
120 |
| - |
121 |
| -### [Number](/dist/Number.d.ts) |
122 |
| - |
123 |
| -```ts |
124 |
| -Number { |
125 |
| - toInt(): number; // converts the current number to an integer (remove the numbers after the dot) |
126 |
| -} |
127 |
| -``` |
128 |
| - |
129 |
| -### [Math](/dist/Math.d.ts) |
130 |
| - |
131 |
| -```ts |
132 |
| -Math { |
133 |
| - static randomBetween(min: number, max: number): number; // generates a random floating point number between min and max |
134 |
| - static randomIntBetween(min: number, max: number): number; // generates a random integer between min and max |
135 |
| -} |
136 |
| -``` |
137 |
| - |
138 |
| -## Example |
139 |
| - |
140 |
| -### Array |
141 |
| - |
142 |
| -```js |
143 |
| -require("missing-native-js-functions"); |
144 |
| - |
145 |
| -const arr = [ |
146 |
| - [0, 1], |
147 |
| - [2, 3], |
148 |
| - [2, 3], |
149 |
| - [2, 3], |
150 |
| - [4, 5], |
151 |
| - [6, 7], |
152 |
| -] |
153 |
| - .flat() |
154 |
| - .unique() |
155 |
| - .shuffle(); |
156 |
| - |
157 |
| -arr.remove(arr.random()); |
158 |
| - |
159 |
| -const first = arr.first(); |
160 |
| -const last = arr.last(); |
161 |
| - |
162 |
| -arr.insert(8); |
163 |
| - |
164 |
| -console.log(arr); |
165 |
| -// -> [4, 7, 5, 1, 6, 2, 3, 8] |
166 |
| - |
167 |
| -class Test { |
168 |
| - constructor() {} |
169 |
| -} |
170 |
| - |
171 |
| -let array = ["test", "test", "test", "no", 2, 15, { 2: 14 }, new Test(), new Test()]; |
172 |
| - |
173 |
| -console.log(array.count(Number)); |
174 |
| -// -> 2 |
175 |
| -console.log(array.count(String)); |
176 |
| -// -> 4 |
177 |
| -console.log(array.count(Object)); |
178 |
| -// -> 1 |
179 |
| -console.log(array.count(Test)); |
180 |
| -// -> 2 |
181 |
| -console.log(array.count(/[a-z]/)); |
182 |
| -// -> 4 |
183 |
| -console.log(array.count("test")); |
184 |
| -// -> 3 |
185 |
| -console.log(array.count(15)); |
186 |
| -// -> 1 |
187 |
| - |
188 |
| -console.log("last number divisable by 2: " + arr.findLast((x) => x % 2 == 0)); |
189 |
| -// -> 8 |
190 |
| -console.log("index of it: " + arr.findLastIndex((x) => x % 2 == 0)); |
191 |
| -// -> 7 |
192 |
| - |
193 |
| -console.log("Similarities between to arrays", [0, 1, 2, 3, 4].similarities([0, 2, 4, 6, 8, 10])); |
194 |
| -// -> [ 0, 2, 4 ] |
195 |
| - |
196 |
| -console.log("Missing values in comparison array", [0, 1, 2, 3, 4, 5].missing([0, 5])); |
197 |
| -// -> [ 1, 2, 3, 4 ] |
198 |
| -``` |
199 |
| - |
200 |
| -### Object |
201 |
| - |
202 |
| -```js |
203 |
| -require("missing-native-js-functions"); |
204 |
| - |
205 |
| -let obj = { |
206 |
| - test: 1, |
207 |
| - tester: 2, |
208 |
| -}; |
209 |
| -obj = obj.map((x) => x * 2); |
210 |
| - |
211 |
| -console.log(obj); |
212 |
| -// -> {test: 2, tester: 4} |
213 |
| - |
214 |
| -let compareObj = { |
215 |
| - test: 2, |
216 |
| - tester: 4, |
217 |
| -}; |
218 |
| - |
219 |
| -obj.forEach(console.log); |
220 |
| -// -> 2 test |
221 |
| -// -> 4 tester |
222 |
| - |
223 |
| -console.log(obj.keys()); |
224 |
| -// -> ["test","tester"] |
225 |
| - |
226 |
| -console.log(obj.entries()); |
227 |
| -// -> [[test,1],[tester,2]] |
228 |
| - |
229 |
| -console.log(obj.stringify()); |
230 |
| -// -> '{"test": 2, "tester": 4}' |
231 |
| -``` |
232 |
| - |
233 |
| -### String |
234 |
| - |
235 |
| -```js |
236 |
| -require("missing-native-js-functions"); |
237 |
| - |
238 |
| -const str1 = "test,hello,1234"; |
239 |
| -const str2 = str1.replaceAll(",", "-"); |
240 |
| - |
241 |
| -console.log({ str1, str2 }); |
242 |
| -// -> {str1: 'test,hello,1234', str2: 'test-hello-1234'} |
243 |
| - |
244 |
| -console.log(str2.capitalize()); |
245 |
| -// -> Test-hello-1234 |
246 |
| - |
247 |
| -const words = ["test", "hello", "1234"]; |
248 |
| -console.log(", ".join(words)); |
249 |
| -// -> test, hello, 1234 |
250 |
| - |
251 |
| -const wordList = "test.hello.1234"; |
252 |
| -console.log(wordList.partition(".")); |
253 |
| -// -> ["test", ".", "hello", ".", "1234"] |
254 |
| - |
255 |
| -"25".toNumber(); |
256 |
| -// -> 25 |
257 |
| - |
258 |
| -"25".toBigInt(); |
259 |
| -// -> 25n |
260 |
| - |
261 |
| -"This is a Thonk Text".swapcase(); |
262 |
| -// -> tHIS IS A tHONK tEXT |
263 |
| - |
264 |
| -"this is a test".title(); |
265 |
| -// -> This Is A Test |
266 |
| - |
267 |
| -'{"this object":"is now parsed"}'.toObject(); |
268 |
| -// -> {"this object":"is now parsed"} |
269 |
| -``` |
270 |
| - |
271 |
| -### Promise |
272 |
| - |
273 |
| -```js |
274 |
| -new Promise((res, rej) => { |
275 |
| - rej("Promised rejected, but caught in console.error"); |
276 |
| -}).caught(); |
277 |
| -// -> will not throw the promise, but log the error in the console |
278 |
| -``` |
279 |
| - |
280 |
| -### Global |
281 |
| - |
282 |
| -```js |
283 |
| -const convert = "this string will be base64 encoded"; |
284 |
| -const converted = btoa("this string was base64 encoded"); |
285 |
| -console.log(convert, btoa(convert)); |
286 |
| -// -> this string will be base64 encoded dGhpcyBzdHJpbmcgd2lsbCBiZSBiYXNlNjQgZW5jb2RlZA== |
287 |
| - |
288 |
| -console.log(atob(converted), converted); |
289 |
| -// -> this string was base64 encoded dGhpcyBzdHJpbmcgd2FzIGJhc2U2NCBlbmNvZGVk |
290 |
| -``` |
291 |
| - |
292 |
| -### Number |
293 |
| - |
294 |
| -```js |
295 |
| -const x = 3.14; |
296 |
| -console.log(`Floating point number ${x} to int: `, x.toInt()); |
297 |
| -// -> 3 |
298 |
| -``` |
299 |
| - |
300 |
| -### Math |
301 |
| - |
302 |
| -```js |
303 |
| -console.log("Random number between 1-10", Math.randomBetween(1, 10)); |
304 |
| -// -> 8.54098 |
305 |
| - |
306 |
| -console.log("Random int between 1-10", Math.randomIntBetween(1, 10)); |
307 |
| -// -> 5 |
308 |
| -``` |
309 | 49 |
|
310 | 50 | ## License
|
311 | 51 |
|
|
0 commit comments