Skip to content

Commit

Permalink
fix: Using safeArrayPush(dest, source) instead of dest.push(...source) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Prokopenko committed Oct 4, 2023
1 parent 13ef4c4 commit 3bf81b5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/orama/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ export async function searchByWhereClause<T extends AnyOrama, ResultDocument = T

if (type === 'Flat') {
if (isArray) {
filtersMap[param].push(...flatFilterArr(node, operation as EnumArrComparisonOperator))
safeArrayPush(filtersMap[param], flatFilterArr(node, operation as EnumArrComparisonOperator))
} else {
filtersMap[param].push(...flatFilter(node, operation as EnumComparisonOperator))
safeArrayPush(filtersMap[param], flatFilter(node, operation as EnumComparisonOperator))
}
continue
}
Expand Down
3 changes: 2 additions & 1 deletion packages/orama/src/components/sorter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
InternalDocumentIDStore,
getInternalDocumentId,
} from './internal-document-id-store.js';
import { safeArrayPush } from "../utils.js";

interface PropertySort<K> {
docs: Map<InternalDocumentID, number>
Expand Down Expand Up @@ -56,7 +57,7 @@ function innerCreate<T extends AnyOrama>(
if (typeof type === 'object' && !Array.isArray(type)) {
// Nested
const ret = innerCreate(orama, sharedInternalDocumentStore, type, sortableDeniedProperties, path)
sorter.sortableProperties.push(...ret.sortableProperties)
safeArrayPush(sorter.sortableProperties, ret.sortableProperties);
sorter.sorts = {
...sorter.sorts,
...ret.sorts,
Expand Down
6 changes: 3 additions & 3 deletions packages/orama/src/trees/flat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InternalDocumentID } from "../components/internal-document-id-store.js"
import { EnumArrComparisonOperator, EnumComparisonOperator, Nullable, ScalarSearchableValue } from "../types.js"
import { intersect } from "../utils.js"
import { intersect, safeArrayPush } from "../utils.js"

export interface FlatTree {
numberToDocumentId: Map<ScalarSearchableValue, InternalDocumentID[]>
Expand Down Expand Up @@ -69,7 +69,7 @@ export function filter(root: FlatTree, operation: EnumComparisonOperator): Inter
for (const v of value) {
const ids = root.numberToDocumentId.get(v)
if (ids) {
result.push(...ids)
safeArrayPush(result, ids)
}
}
return result
Expand All @@ -85,7 +85,7 @@ export function filter(root: FlatTree, operation: EnumComparisonOperator): Inter
}
const ids = root.numberToDocumentId.get(key)
if (ids) {
result.push(...ids)
safeArrayPush(result, ids)
}
}
return result
Expand Down
3 changes: 2 additions & 1 deletion packages/orama/tests/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Index } from '../src/components/index.js'
import { getInternalDocumentId } from '../src/components/internal-document-id-store.js'
import { AnyDocument, create, insert, insertMultiple, search } from '../src/index.js'
import dataset from './datasets/events.json' assert { type: 'json' }
import { safeArrayPush } from "../src/utils"

t.test('insert method', t => {
t.test('should correctly insert and retrieve data', async t => {
Expand Down Expand Up @@ -320,7 +321,7 @@ t.test('insert method', t => {
{ boolean: {} },
{ boolean: [] },
]
invalidDocuments.push(...invalidDocuments.map(d => ({ inner: { ...d } })))
safeArrayPush(invalidDocuments, invalidDocuments.map(d => ({ inner: { ...d } })))
for (const doc of invalidDocuments) {
await t.rejects(insert(db, doc))
}
Expand Down

0 comments on commit 3bf81b5

Please sign in to comment.