Skip to content

Commit

Permalink
fix: updated nuxt ids function
Browse files Browse the repository at this point in the history
  • Loading branch information
ravgeetdhillon committed May 19, 2022
1 parent 0271cb4 commit 644e59b
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions utils/id.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { nanoid } from 'nanoid'

const addNuxtId = (entity) => {
if (Array.isArray(entity)) {
return entity.map((item) => ({
...item,
nid: nanoid(),
}))
} else if (typeof entity === 'object' && !Array.isArray(entity) && entity !== null) {
return {
...entity,
nid: nanoid(),
}
const isObject = (val) => val && typeof val === 'object' && !Array.isArray(val)

const addNuxtId = (obj) => {
if (!obj || typeof obj !== 'object') return
if (isObject(obj)) {
obj.nid = nanoid()
}
Object.keys(obj).forEach((k) => addNuxtId(obj[k]))
return obj
}

export { addNuxtId }

0 comments on commit 644e59b

Please sign in to comment.