diff --git a/package-lock.json b/package-lock.json index d7ee16f..563c29c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.4.0", + "version": "1.5.0", "license": "MIT", "dependencies": { "json-mask": "2.0.0", diff --git a/package.json b/package.json index e9150d8..2feb157 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.4.0", + "version": "1.5.0", "description": "JavaScript filesystem SDK to query data synced via @contentstack/datasync-content-store-filesystem", "main": "dist/index.js", "scripts": { diff --git a/src/stack.ts b/src/stack.ts index 4f063e9..2abef91 100755 --- a/src/stack.ts +++ b/src/stack.ts @@ -1627,6 +1627,26 @@ export class Stack { } } + /** + * @private + * @method addToShelfIfNotExists + * @description Helper function to add entry to shelf only if it doesn't already exist + * @param {IShelf[]} shelf - The shelf array to add to + * @param {any} path - The path reference + * @param {number} position - The position in the path + * @param {string} uid - The unique identifier + */ + private addToShelfIfNotExists(shelf: IShelf[], path, position, uid) { + const exists = shelf.some(entry => + entry.path === path && + entry.position === position && + entry.uid === uid + ); + if (!exists) { + shelf.push({ path, position, uid }); + } + } + // tslint:disable-next-line: max-line-length private fetchPathDetails(data: any, locale: string, pathArr: string[], queryBucket: IQuery, shelf, assetsOnly = false, parent, pos, counter = 0) { if (counter === (pathArr.length)) { @@ -1641,11 +1661,7 @@ export class Stack { uid: elem, }) - shelf.push({ - path: data, - position: idx, - uid: elem, - }) + this.addToShelfIfNotExists(shelf, data, idx, elem); } else if (elem && typeof elem === 'object' && elem.hasOwnProperty('_content_type_uid')) { queryBucket.$or.push({ _content_type_uid: elem._content_type_uid, @@ -1653,11 +1669,7 @@ export class Stack { uid: elem.uid, }) - shelf.push({ - path: data, - position: idx, - uid: elem.uid, - }) + this.addToShelfIfNotExists(shelf, data, idx, elem.uid); } }) } else if (typeof data === 'object') { @@ -1668,11 +1680,7 @@ export class Stack { uid: data.uid, }) - shelf.push({ - path: parent, - position: pos, - uid: data.uid, - }) + this.addToShelfIfNotExists(shelf, parent, pos, data.uid); } } } else if (typeof data === 'string') { @@ -1683,11 +1691,7 @@ export class Stack { uid: data, }) - shelf.push({ - path: parent, - position: pos, - uid: data, - }) + this.addToShelfIfNotExists(shelf, parent, pos, data); } } else { const currentField = pathArr[counter] diff --git a/typings/stack.d.ts b/typings/stack.d.ts index 069397f..1a35fbe 100644 --- a/typings/stack.d.ts +++ b/typings/stack.d.ts @@ -551,6 +551,16 @@ export declare class Stack { private includeReferenceIteration; private subIncludeReferenceIteration; private getReferencePath; + /** + * @private + * @method addToShelfIfNotExists + * @description Helper function to add entry to shelf only if it doesn't already exist + * @param {IShelf[]} shelf - The shelf array to add to + * @param {any} path - The path reference + * @param {number} position - The position in the path + * @param {string} uid - The unique identifier + */ + private addToShelfIfNotExists; private fetchPathDetails; private fetchDocuments; private includeAssetsOnly;