Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 978b53a

Browse files
committed
Initial commit. Added tests with jest and a README.
0 parents commit 978b53a

22 files changed

+8028
-0
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

‎.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/javascript-utilities.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/jsLibraryMappings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/markdown.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# JavaScript Utilities
2+
3+
This package utilizes JavaScript snippets from the [30 Seconds of Code]() project.

‎dist/Objects.d.ts

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
/**
2+
* Retrieves a set of properties indicated by the given selectors from an object.
3+
*
4+
* Use Array.prototype.map() for each selector, String.prototype.replace() to replace square brackets with dots.
5+
* Use String.prototype.split() to split each selector.
6+
* Use Array.prototype.filter() to remove empty values and Array.prototype.reduce() to get the value indicated by each selector.
7+
* @param from - The object you would like to select values from
8+
* @param selectors - Can use object notation, key access, or both
9+
* @returns {*[]}
10+
* @example
11+
* const obj = {
12+
* selector: { to: { val: 'val to select' }},
13+
* target: [1, 2, { a: 'test' }],
14+
* };
15+
* get(obj, 'selector.to.val', 'target[0]', 'target[2].a');
16+
* // ['val to select', 1, 'test']
17+
*/
18+
export declare const get: (from: any, ...selectors: any[]) => any[];
19+
/**
20+
* Deeply merges two objects, using a function to handle keys present in both.
21+
*
22+
* Use Object.keys() to get the keys of both objects, create a Set from them and use the spread operator (...) to create an array of all the unique keys.
23+
* Use Array.prototype.reduce() to add each unique key to the object, using fn to combine the values of the two given objects.
24+
* @param a - Object1
25+
* @param b - Object2
26+
* @param fn - A function that you define that tells deepMerge what to do with a key if that key exists in both objects.
27+
* @returns {*}
28+
* @example
29+
* deepMerge(
30+
* { a: true, b: { c: [1, 2, 3] }},
31+
* { a: false, b: { d: [1, 2, 3] }},
32+
* (key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b))
33+
* );
34+
* // { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] }}
35+
*/
36+
export declare const deepMerge: (a: any, b: any, fn: any) => {};
37+
/**
38+
* Converts an array of objects to a comma-separated values (CSV) string that contains only the columns specified.
39+
*
40+
* Use Array.prototype.join() to combine all the names in columns to create the first row, using the provided delimiter.
41+
* Use Array.prototype.map() and Array.prototype.reduce() to create a row for each object. Substitute non-existent values with empty strings and only mapping values in columns.
42+
* Use Array.prototype.join() to combine all rows into a string, separating each row with a newline (\n).
43+
* Omit the third argument, delimiter, to use a default delimiter of ','.
44+
* @param arr - An array of objects. Any keys present in an object that doesn't have a corresponding value in the "columns" parameter will be omitted from the resulting SCV string.
45+
* @param columns - An array of columns. The strings listed here should match keys from the provided objects.
46+
* @param [delimiter] - Optional. Defaults to ",". Supply this argument if you'd like a different delimiter, such as ";", "\t", etc.
47+
* @returns {string}
48+
* @constructor
49+
* @example
50+
* JSONtoCSV(
51+
* [{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }],
52+
* ['a', 'b']
53+
* );
54+
* // 'a,b\n"1","2"\n"3","4"\n"6",""\n"","7"'
55+
*
56+
* JSONtoCSV(
57+
* [{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }, { b: 7 }],
58+
* ['a', 'b'],
59+
* ';'
60+
* );
61+
* // 'a;b\n"1";"2"\n"3";"4"\n"6";""\n"";"7"'
62+
*/
63+
export declare const JSONtoCSV: (arr: any, columns: any, delimiter?: string) => string;
64+
/**
65+
* Replaces the names of multiple object keys with the values provided.
66+
*
67+
* Use Object.keys() in combination with Array.prototype.reduce() and the spread operator (...) to get the object's keys and rename them according to keysMap.
68+
* @param keysMap - A key value object where the key matches a key in the object provided, and the value is the new name desired for the specified kep.
69+
* @param obj - The object for which you would like keys renamed.
70+
* @example
71+
* const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 };
72+
* renameKeys({ name: 'firstName', job: 'passion' }, obj);
73+
* // { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }
74+
*/
75+
export declare const renameKeys: (keysMap: any, obj: any) => {};
76+
/**
77+
* Converts a comma-separated values (CSV) string to a 2D array of objects. The first row of the string is used as the title row.
78+
*
79+
* Use Array.prototype.indexOf() to find the first newline character (\n).
80+
* Use Array.prototype.slice() to remove the first row (title row) and String.prototype.split() to separate it into values, using the provided delimiter.
81+
* Use String.prototype.split() to create a string for each row.
82+
* Use String.prototype.split() to separate the values in each row, using the provided delimiter.
83+
* Use Array.prototype.reduce() to create an object for each row's values, with the keys parsed from the title row.
84+
* Omit the second argument, delimiter, to use a default delimiter of ",".
85+
* @param data - CSV data as a string
86+
* @param [delimiter] - Optional. Defaults to ",". Tells the function what delimiter to expect in the provided csv input.
87+
* @constructor
88+
* @example
89+
* CSVToJSON('col1,col2\na,b\nc,d');
90+
* // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
91+
*
92+
* CSVToJSON('col1;col2\na;b\nc;d', ';');
93+
* // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
94+
*/
95+
export declare const CSVToJSON: (data: any, delimiter?: string) => any;
96+
/**
97+
* Deeply removes all falsy values from an object or array.
98+
*
99+
* Use recursion.
100+
* Initialize the iterable data, using Array.isArray(), Array.prototype.filter() and Boolean for arrays in order to avoid sparse arrays.
101+
* Use Object.keys() and Array.prototype.reduce() to iterate over each key with an appropriate initial value.
102+
* Use Boolean to determine the truthiness of each key's value and add it to the accumulator if it's truthy.
103+
* Use typeof to determine if a given value is an object and call the function again to deeply compact it.
104+
* @param val - The object to compact
105+
* @example
106+
* const obj = {
107+
* a: null,
108+
* b: false,
109+
* c: true,
110+
* d: 0,
111+
* e: 1,
112+
* f: '',
113+
* g: 'a',
114+
* h: [null, false, '', true, 1, 'a'],
115+
* i: { j: 0, k: false, l: 'a' }
116+
* };
117+
* compactObject(obj);
118+
* // { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' }}
119+
*/
120+
export declare const compactObject: (val: any) => {};
121+
/**
122+
* Groups the elements of an array based on the given function and returns the count of elements in each group.
123+
*
124+
* Use Array.prototype.map() to map the values of an array to a function or property name.
125+
* Use Array.prototype.reduce() to create an object, where the keys are produced from the mapped results.
126+
* @param arr - An array for which you would like a group count specified by the "fn" parameter.
127+
* @param fn - A function or other method by which to specify how you would like the count grouped.
128+
* @example
129+
* countBy([6.1, 4.2, 6.3], Math.floor);
130+
* // {4: 1, 6: 2}
131+
*
132+
* countBy(['one', 'two', 'three'], 'length');
133+
* // {3: 2, 5: 1}
134+
*
135+
* countBy([{ count: 5 }, { count: 10 }, { count: 5 }], x => x.count)
136+
* // {5: 2, 10: 1}
137+
*/
138+
export declare const countBy: (arr: any, fn: any) => any;
139+
/**
140+
* Groups the elements of an array based on the given function.
141+
*
142+
* Use Array.prototype.map() to map the values of the array to a function or property name.
143+
* Use Array.prototype.reduce() to create an object, where the keys are produced from the mapped results.
144+
* @param arr - An array for which you would like the items grouped according to the specified "fn" parameter.
145+
* @param fn - A function or other method by which to specify how you would like the items grouped.
146+
* @example
147+
* groupBy([6.1, 4.2, 6.3], Math.floor);
148+
* // {4: [4.2], 6: [6.1, 6.3]}
149+
*
150+
* groupBy(['one', 'two', 'three'], 'length');
151+
* // {3: ['one', 'two'], 5: ['three']}
152+
*/
153+
export declare const groupBy: (arr: any, fn: any) => any;
154+
/**
155+
* Generates an object from the given query string or URL.
156+
*
157+
* Use String.prototype.split() to get the params from the given url.
158+
* Use the URLSearchParams constructor to create an appropriate object and convert it to an array of key-value pairs using the spread operator (...).
159+
* Use Array.prototype.reduce() to convert the array of key-value pairs into an object.
160+
* @param url
161+
* @example
162+
* queryStringToObject('https://google.com?page=1&count=10');
163+
* // {page: '1', count: '10'}
164+
*/
165+
export declare const queryStringToObject: (url: any) => {};
166+
/**
167+
* Serializes a JSON object containing circular references into a JSON format.
168+
*
169+
* Create a WeakSet to store and check seen values, using WeakSet.prototype.add() and WeakSet.prototype.has().
170+
* Use JSON.stringify() with a custom replacer function that omits values already in seen, adding new values as necessary.
171+
* ⚠️ NOTICE: This function finds and removes circular references, which causes circular data loss in the serialized JSON.
172+
* @param obj
173+
* @example
174+
* const obj = { n: 42 };
175+
* obj.obj = obj;
176+
* stringifyCircularJSON(obj);
177+
* // '{"n":42}'
178+
*/
179+
export declare const stringifyCircularJSON: (obj: any) => string;
180+
/**
181+
* Creates an array of key-value pair arrays from an object or other iterable.
182+
*
183+
* Check if Symbol.iterator is defined and, if so, use Array.prototype.entries() to get an iterator for the given iterable.
184+
* Use Array.from() to convert the result to an array of key-value pair arrays.
185+
* If Symbol.iterator is not defined for obj, use Object.entries() instead.
186+
* @param obj
187+
* @example
188+
* toPairs({ a: 1, b: 2 });
189+
* // [['a', 1], ['b', 2]]
190+
*
191+
* toPairs([2, 4, 8]);
192+
* // [[0, 2], [1, 4], [2, 8]]
193+
*
194+
* toPairs('shy');
195+
* // [['0', 's'], ['1', 'h'], ['2', 'y']]
196+
*
197+
* toPairs(new Set(['a', 'b', 'c', 'a']));
198+
* // [['a', 'a'], ['b', 'b'], ['c', 'c']]
199+
*/
200+
export declare const toPairs: (obj: any) => unknown[];
201+
/**
202+
* Applies a function against an accumulator and each key in the object (from left to right).
203+
*
204+
* Use Object.keys() to iterate over each key in the object.
205+
* Use Array.prototype.reduce() to apply the specified function against the given accumulator.
206+
* @param obj
207+
* @param fn
208+
* @param acc
209+
* @example
210+
* transform(
211+
* { a: 1, b: 2, c: 1 },
212+
* (r, v, k) => {
213+
* (r[v] || (r[v] = [])).push(k);
214+
* return r;
215+
* },
216+
* {}
217+
* );
218+
* // { '1': ['a', 'c'], '2': ['b'] }
219+
*/
220+
export declare const transform: (obj: any, fn: any, acc: any) => any;
221+
/**
222+
* Unflatten an object with the paths for keys.
223+
*
224+
* Use nested Array.prototype.reduce() to convert the flat path to a leaf node.
225+
* Use String.prototype.split() to split each key with a dot delimiter and Array.prototype.reduce() to add objects against the keys.
226+
* If the current accumulator already contains a value against a particular key, return its value as the next accumulator.
227+
* Otherwise, add the appropriate key-value pair to the accumulator object and return the value as the accumulator.
228+
* @param obj
229+
* @example
230+
* unflattenObject({ 'a.b.c': 1, d: 1 });
231+
* // { a: { b: { c: 1 }}, d: 1 }
232+
*
233+
* unflattenObject({ 'a.b': 1, 'a.c': 2, d: 3 });
234+
* // { a: { b: 1, c: 2 }, d: 3 }
235+
*
236+
* unflattenObject({ 'a.b.0': 8, d: 3 });
237+
* // { a: { b: [ 8 ] }, d: 3 }
238+
*
239+
*/
240+
export declare const unflattenObject: (obj: any) => {};
241+
/**
242+
* Creates a generator, that walks through all the keys of a given object.
243+
*
244+
* Use recursion.
245+
* Define a generator function, `walk`, that takes an object and an array of keys.
246+
* Use a `for...of` loop and `Object.keys()` to iterate over the keys of the object.
247+
* Use `typeof` to check if each value in the given object is itself an object.
248+
* If so, use the `yield*` expression to recursively delegate to the same generator
249+
* function, `walk`, appending the current `key` to the array of keys. Otherwise,
250+
* `yield` an array of keys representing the current path and the value of the given `key`.
251+
* Use the `yield*` expression to delegate to the `walk` generator function.
252+
* @param obj
253+
* @example
254+
* const obj = {
255+
* a: 10,
256+
* b: 20,
257+
* c: {
258+
* d: 10,
259+
* e: 20,
260+
* f: [30, 40]
261+
* },
262+
* g: [
263+
* {
264+
* h: 10,
265+
* i: 20
266+
* },
267+
* {
268+
* j: 30
269+
* },
270+
* 40
271+
* ]
272+
* };
273+
* [...walkThrough(obj)];
274+
*
275+
* // [
276+
* // [['a'], 10],
277+
* // [['b'], 20],
278+
* // [['c', 'd'], 10],
279+
* // [['c', 'e'], 20],
280+
* // [['c', 'f', '0'], 30],
281+
* // [['c', 'f', '1'], 40],
282+
* // [['g', '0', 'h'], 10],
283+
* // [['g', '0', 'i'], 20],
284+
* // [['g', '1', 'j'], 30],
285+
* // [['g', '2'], 40]
286+
* // ]
287+
*
288+
*/
289+
export declare const walkThrough: (obj: any) => Generator<any, void, any>;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /