@magnetarjs/utils
Advanced tools
Comparing version 0.12.13 to 0.13.0
@@ -47,5 +47,5 @@ "use strict"; | ||
// src/internal/dataHelpers.ts | ||
var import_fast_sort = require("fast-sort"); | ||
var import_is_what2 = require("is-what"); | ||
var import_path_to_prop = require("path-to-prop"); | ||
var import_fast_sort = require("fast-sort"); | ||
@@ -66,48 +66,58 @@ // src/internal/parseValueForFilters.ts | ||
// src/internal/dataHelpers.ts | ||
function passesWhere(docData, whereQuery) { | ||
const [fieldPath, operator, expectedValue] = whereQuery; | ||
const valueAtFieldPath = parseValueForFilters((0, import_path_to_prop.getProp)(docData, fieldPath)); | ||
let passes = false; | ||
switch (operator) { | ||
case "==": | ||
passes = valueAtFieldPath == expectedValue; | ||
break; | ||
case "!=": | ||
passes = valueAtFieldPath != expectedValue; | ||
break; | ||
case "<": | ||
passes = valueAtFieldPath < expectedValue; | ||
break; | ||
case "<=": | ||
passes = valueAtFieldPath <= expectedValue; | ||
break; | ||
case ">": | ||
passes = valueAtFieldPath > expectedValue; | ||
break; | ||
case ">=": | ||
passes = valueAtFieldPath >= expectedValue; | ||
break; | ||
case "in": | ||
passes = (0, import_is_what2.isArray)(expectedValue) && expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case "not-in": | ||
passes = (0, import_is_what2.isArray)(expectedValue) && !expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case "array-contains": | ||
passes = (0, import_is_what2.isArray)(valueAtFieldPath) && valueAtFieldPath.includes(expectedValue); | ||
break; | ||
case "array-contains-any": | ||
passes = (0, import_is_what2.isArray)(valueAtFieldPath) && valueAtFieldPath.some((v) => (0, import_is_what2.isArray)(expectedValue) && expectedValue.includes(v)); | ||
break; | ||
default: | ||
throw new Error("invalid operator"); | ||
} | ||
return passes; | ||
} | ||
function filterDataPerClauses(collectionDB, clauses) { | ||
const { where = [], orderBy = [], limit, startAfter } = clauses; | ||
if (!where.length && !orderBy.length && !(0, import_is_what2.isNumber)(limit) && !startAfter) | ||
const queryClauses = clauses.query || []; | ||
const whereClauses = clauses.where || []; | ||
const orderByClauses = clauses.orderBy || []; | ||
const { limit, startAfter } = clauses; | ||
if (!queryClauses.length && !whereClauses.length && !orderByClauses.length && !(0, import_is_what2.isNumber)(limit) && !startAfter) { | ||
return collectionDB; | ||
} | ||
let entries = []; | ||
collectionDB.forEach((docData, docId) => { | ||
const passesWhereFilters = where.every((whereQuery) => { | ||
const [fieldPath, operator, expectedValue] = whereQuery; | ||
const valueAtFieldPath = parseValueForFilters((0, import_path_to_prop.getProp)(docData, fieldPath)); | ||
let passes = false; | ||
switch (operator) { | ||
case "==": | ||
passes = valueAtFieldPath == expectedValue; | ||
break; | ||
case "!=": | ||
passes = valueAtFieldPath != expectedValue; | ||
break; | ||
case "<": | ||
passes = valueAtFieldPath < expectedValue; | ||
break; | ||
case "<=": | ||
passes = valueAtFieldPath <= expectedValue; | ||
break; | ||
case ">": | ||
passes = valueAtFieldPath > expectedValue; | ||
break; | ||
case ">=": | ||
passes = valueAtFieldPath >= expectedValue; | ||
break; | ||
case "in": | ||
passes = (0, import_is_what2.isArray)(expectedValue) && expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case "not-in": | ||
passes = (0, import_is_what2.isArray)(expectedValue) && !expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case "array-contains": | ||
passes = (0, import_is_what2.isArray)(valueAtFieldPath) && valueAtFieldPath.includes(expectedValue); | ||
break; | ||
case "array-contains-any": | ||
passes = (0, import_is_what2.isArray)(valueAtFieldPath) && valueAtFieldPath.some((v) => (0, import_is_what2.isArray)(expectedValue) && expectedValue.includes(v)); | ||
break; | ||
default: | ||
throw new Error("invalid operator"); | ||
} | ||
return passes; | ||
}); | ||
const passesQuery = queryClauses.every((queryClause) => passesQuery(docData, queryClause)); | ||
if (!passesQuery) | ||
return; | ||
const passesWhereFilters = whereClauses.every( | ||
(whereClause) => passesWhere(docData, whereClause) | ||
); | ||
if (!passesWhereFilters) | ||
@@ -117,3 +127,3 @@ return; | ||
}); | ||
const by = orderBy.reduce((carry, [path, direction = "asc"]) => { | ||
const by = orderByClauses.reduce((carry, [path, direction = "asc"]) => { | ||
const sorter = { | ||
@@ -125,5 +135,5 @@ [direction]: (entry) => (0, import_path_to_prop.getProp)(entry[1], path) | ||
}, []); | ||
entries = orderBy.length ? (0, import_fast_sort.sort)(entries).by(by) : entries; | ||
if (startAfter && orderBy.length) { | ||
const orderByKeys = orderBy.map(([path]) => path); | ||
entries = orderByClauses.length ? (0, import_fast_sort.sort)(entries).by(by) : entries; | ||
if (startAfter && orderByClauses.length) { | ||
const orderByKeys = orderByClauses.map(([path]) => path); | ||
const startAfterValues = Array.isArray(startAfter) ? startAfter : orderByKeys.map((key) => startAfter[key]); | ||
@@ -164,2 +174,11 @@ if (startAfterValues.length > orderByKeys.length) { | ||
} | ||
function stringifyQueryClause(q) { | ||
return "or" in q ? `or(${(0, import_is_what3.isArray)(q.or) ? q.or.map((where) => stringifyWhereClause(where)).join(", ") : stringifyQueryClause(q.or)})` : `and(${(0, import_is_what3.isArray)(q.and) ? q.and.map((where) => stringifyWhereClause(where)).join(", ") : stringifyQueryClause(q.and)})`; | ||
} | ||
function clean(c) { | ||
return JSON.stringify(c).replaceAll(`"`, `'`); | ||
} | ||
function stringifyWhereClause(w) { | ||
return `where(${w.map(clean).join(", ")})`; | ||
} | ||
var logger = function({ | ||
@@ -175,9 +194,9 @@ payload, | ||
return; | ||
const { where, orderBy, limit } = pluginModuleConfig; | ||
const { query, where, orderBy, limit } = pluginModuleConfig; | ||
const docOrCollection = docId ? `doc('${path}')` : `collection('${path}')`; | ||
const cleanClause = (c) => JSON.stringify(c).replaceAll(`"`, `'`); | ||
const _where = !(0, import_is_what3.isFullArray)(where) ? [] : where.map((whereClause) => `where(${whereClause.map(cleanClause).join(", ")})`); | ||
const _orderBy = !(0, import_is_what3.isFullArray)(orderBy) ? [] : orderBy.map((o) => `orderBy(${o.map(cleanClause).join(", ")})`); | ||
const _query = !(0, import_is_what3.isFullArray)(query) ? [] : query.map((queryClause) => `where(${stringifyQueryClause(queryClause)}`); | ||
const _where = !(0, import_is_what3.isFullArray)(where) ? [] : where.map((whereClause) => stringifyWhereClause(whereClause)); | ||
const _orderBy = !(0, import_is_what3.isFullArray)(orderBy) ? [] : orderBy.map((o) => `orderBy(${o.map(clean).join(", ")})`); | ||
const _limit = !(0, import_is_what3.isNumber)(limit) ? [] : [`limit(${limit})`]; | ||
const message = ["db", docOrCollection, ..._where, ..._orderBy, ..._limit].join("."); | ||
const message = ["db", docOrCollection, ..._query, ..._where, ..._orderBy, ..._limit].join("."); | ||
const action = payload === void 0 ? [`${actionName}()`] : [`${actionName}(`, payload, `)`]; | ||
@@ -184,0 +203,0 @@ logWithFlair(message, ...action); |
{ | ||
"name": "@magnetarjs/utils", | ||
"version": "0.12.13", | ||
"version": "0.13.0", | ||
"sideEffects": false, | ||
@@ -27,3 +27,3 @@ "description": "Magnetar utils like a logger for easier development", | ||
"path-to-prop": "^2.0.2", | ||
"@magnetarjs/types": "0.12.13" | ||
"@magnetarjs/types": "0.13.0" | ||
}, | ||
@@ -52,2 +52,3 @@ "keywords": [ | ||
"scripts": { | ||
"typecheck": "tsc --noEmit", | ||
"build": "tsup src/index.ts --clean --format esm,cjs --dts", | ||
@@ -54,0 +55,0 @@ "dev": "pnpm build --watch", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16943
396
+ Added@magnetarjs/types@0.13.0(transitive)
- Removed@magnetarjs/types@0.12.13(transitive)
Updated@magnetarjs/types@0.13.0