Created
November 26, 2022 19:32
-
-
Save JenniferFuBook/b203ed8f09f166529dc1dd19821d8c7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
adjectives, | |
animals, | |
colors, | |
countries, | |
uniqueNamesGenerator, | |
} from 'unique-names-generator'; | |
export const createName = () => | |
uniqueNamesGenerator({ | |
dictionaries: [colors, adjectives, animals], | |
style: 'capital', | |
separator: ' ', | |
}); | |
export const createCountryName = () => | |
uniqueNamesGenerator({ | |
dictionaries: [countries], | |
style: 'capital', | |
}); | |
export const createNewRow = () => { | |
const name = createName(); | |
return { | |
key: name, | |
name, | |
country: createCountryName(), | |
}; | |
}; | |
export const createTableData = (rowCount) => | |
Array(rowCount) | |
.fill(1) | |
.map(() => createNewRow()); | |
export const removeRowFromTableData = (data = [], key) => { | |
if (key) { | |
const index = data.findIndex((item) => item.key === key); | |
if (index !== -1) { | |
data.splice(index, 1); | |
} | |
} | |
}; | |
export const getTableRowCount = (data = []) => | |
data.reduce( | |
(count, item) => | |
item.children ? count + getTableRowCount(item.children) + 1 : count + 1, | |
0 | |
); | |
export const getAllParentNodeKeys = (data = []) => | |
data.reduce( | |
(list, item) => | |
item.children | |
? [...list, item.key, ...getAllParentNodeKeys(item.children)] | |
: list, | |
[] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment