Download spectrum/shared/unique-elements.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 813 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spectrum/shared/unique-elements.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spectrum/shared/unique-elements.js
-
curl -L -o unique-elements.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/spectrum/shared/unique-elements.js
813 Bytes
| // @flow | |
| // Get unique elements in an array, even if they're complex like nested arrays or objects | |
| // NOTE: Uses ES6 Set, so DO NOT USE on the frontend | |
| const unique = (array: Array<mixed>): Array<mixed> => { | |
| if (array.length <= 1) return array; | |
| // Turn all elements into strings, including arrays | |
| const stringElements = array.map(key => JSON.stringify(key)); | |
| // Get the unique elements by using an ES6 Set | |
| const uniqueElements = [...new Set(stringElements)]; | |
| // Turn the non-string elements back into non-strings | |
| return uniqueElements.map(key => { | |
| // If it's e.g. an array, this will return the original array | |
| try { | |
| return JSON.parse(key); | |
| } catch (err) { | |
| // Oh, it was a string after all, let's just return that. | |
| return key; | |
| } | |
| }); | |
| }; | |
| export default unique; | |