@sanity/import
Version:
Import documents to a Sanity dataset
36 lines (35 loc) • 1.09 kB
JavaScript
import { getRandomValues } from 'node:crypto';
function isPlainObject(value) {
if (typeof value !== 'object' || value === null) return false;
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}
// Note: Mutates in-place
function assignArrayKeys(obj) {
if (Array.isArray(obj)) {
for (const item of obj){
if (isPlainObject(item) && !('_key' in item)) {
;
item._key = generateKey();
}
assignArrayKeys(item);
}
return obj;
}
if (isPlainObject(obj)) {
const plainObj = obj;
for (const key of Object.keys(plainObj)){
assignArrayKeys(plainObj[key]);
}
return obj;
}
return obj;
}
function generateKey(length = 8) {
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
const bytes = new Uint8Array(length);
getRandomValues(bytes);
return Array.from(bytes, (b)=>chars[b % chars.length]).join('');
}
export { assignArrayKeys };
//# sourceMappingURL=assignArrayKeys.js.map