From 2f13177dcda9c027d2e4590abf61241cf1042ed7 Mon Sep 17 00:00:00 2001 From: Mike Cornwell Date: Mon, 27 Jan 2025 15:02:40 -0500 Subject: [PATCH] fix(threeitize): expose the threeitize function --- package.json | 2 +- src/orm/query.ts | 21 +++++++++++++++++++++ src/utils.ts | 15 --------------- test/src/orm/query.test.ts | 36 ++++++++++++++++++++++++++++++++++++ test/src/utils.test.ts | 36 ------------------------------------ 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 1d0a745..82d9c56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "functional-models", - "version": "3.0.8", + "version": "3.0.9", "description": "Functional models is ooey gooey framework for building and using awesome models EVERYWHERE.", "main": "index.js", "types": "index.d.ts", diff --git a/src/orm/query.ts b/src/orm/query.ts index eb89b78..285d235 100644 --- a/src/orm/query.ts +++ b/src/orm/query.ts @@ -22,6 +22,8 @@ import { SubBuilderFunction, } from './types' +const THREE = 3 + const _objectize = (key: string, value: T) => { return value ? { @@ -378,6 +380,24 @@ const booleanQuery = (key: string, value: boolean | undefined | null) => equalitySymbol: EqualitySymbol.eq, }) +/** + * A useful utility for processing {@link QueryTokens} with a {@link DatastoreAdapter} + * Takes the first 3 values (property, LINK, property) and then shifts the list left by 2, so that it can create another property, LINK, property + * @param data - The list of values + */ +const threeitize = (data: T[]): T[][] => { + if (data.length === 0 || data.length === 1) { + return [] + } + if (data.length % 2 === 0) { + throw new Error('Must be an odd number of 3 or greater.') + } + const three = data.slice(0, THREE) + const rest = data.slice(2) + const moreThrees = threeitize(rest) + return [three, ...moreThrees] +} + export { queryBuilder, take, @@ -393,4 +413,5 @@ export { textQuery, numberQuery, booleanQuery, + threeitize, } diff --git a/src/utils.ts b/src/utils.ts index 7303078..6df3972 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,7 +5,6 @@ import AsyncLock from 'async-lock' const HEX = 16 const FOUR = 4 const FIFTEEN = 15 -const THREE = 3 const getRandomValues = (): Uint8Array => { const array = new Uint8Array(1) @@ -117,19 +116,6 @@ const memoizeAsync = >(method: (...args: A) => T) => { /* eslint-enable functional/no-let */ } -const threeitize = (data: T[]): T[][] => { - if (data.length === 0 || data.length === 1) { - return [] - } - if (data.length % 2 === 0) { - throw new Error('Must be an odd number of 3 or greater.') - } - const three = data.slice(0, THREE) - const rest = data.slice(2) - const moreThrees = threeitize(rest) - return [three, ...moreThrees] -} - export { loweredTitleCase, toTitleCase, @@ -140,5 +126,4 @@ export { flowFindFirst, memoizeSync, memoizeAsync, - threeitize, } diff --git a/test/src/orm/query.test.ts b/test/src/orm/query.test.ts index 0a21581..08da14f 100644 --- a/test/src/orm/query.test.ts +++ b/test/src/orm/query.test.ts @@ -17,9 +17,45 @@ import { take, textQuery, booleanQuery, + threeitize, } from '../../../src' describe('/src/orm/query.ts', () => { + describe('#threeitize()', () => { + it('should throw an exception if there are 4 values', () => { + assert.throws(() => { + threeitize(['a', 'b', 'c', 'd']) + }, 'Must be an odd number of 3 or greater') + }) + it('should allow 5 values', () => { + const actual = threeitize(['a', 'b', 'c', 'd', 'e']) + const expected = [ + ['a', 'b', 'c'], + ['c', 'd', 'e'], + ] + assert.deepEqual(actual, expected) + }) + it('should throw an exception if there are 6 values', () => { + assert.throws(() => { + threeitize(['a', 'b', 'c', 'd', 'e', 'f']) + }, 'Must be an odd number of 3 or greater') + }) + it('should throw an exception if there are 8 values', () => { + assert.throws(() => { + threeitize(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) + }, 'Must be an odd number of 3 or greater') + }) + it('should create the expected structure by walking down and grouping', () => { + const actual = threeitize(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) + const expected = [ + ['a', 'b', 'c'], + ['c', 'd', 'e'], + ['e', 'f', 'g'], + ['g', 'h', 'i'], + ] + assert.deepEqual(actual, expected) + }) + }) describe('#booleanQuery()', () => { it('should create an expected boolean property query', () => { const actual = booleanQuery('my-key', false) diff --git a/test/src/utils.test.ts b/test/src/utils.test.ts index e60f5a8..8d5a3be 100644 --- a/test/src/utils.test.ts +++ b/test/src/utils.test.ts @@ -8,45 +8,9 @@ import { isPromise, flowFindFirst, memoizeAsync, - threeitize, } from '../../src/utils' describe('/src/utils.ts', () => { - describe('#threeitize()', () => { - it('should throw an exception if there are 4 values', () => { - assert.throws(() => { - threeitize(['a', 'b', 'c', 'd']) - }, 'Must be an odd number of 3 or greater') - }) - it('should allow 5 values', () => { - const actual = threeitize(['a', 'b', 'c', 'd', 'e']) - const expected = [ - ['a', 'b', 'c'], - ['c', 'd', 'e'], - ] - assert.deepEqual(actual, expected) - }) - it('should throw an exception if there are 6 values', () => { - assert.throws(() => { - threeitize(['a', 'b', 'c', 'd', 'e', 'f']) - }, 'Must be an odd number of 3 or greater') - }) - it('should throw an exception if there are 8 values', () => { - assert.throws(() => { - threeitize(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) - }, 'Must be an odd number of 3 or greater') - }) - it('should create the expected structure by walking down and grouping', () => { - const actual = threeitize(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) - const expected = [ - ['a', 'b', 'c'], - ['c', 'd', 'e'], - ['e', 'f', 'g'], - ['g', 'h', 'i'], - ] - assert.deepEqual(actual, expected) - }) - }) describe('#memoizeAsync()', () => { it('should only call the method passed in once even after two calls', async () => { const method = sinon.stub().returns('hello-world')