Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 21 additions & 0 deletions src/orm/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
SubBuilderFunction,
} from './types'

const THREE = 3

const _objectize = <T>(key: string, value: T) => {
return value
? {
Expand Down Expand Up @@ -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 = <T>(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,
Expand All @@ -393,4 +413,5 @@ export {
textQuery,
numberQuery,
booleanQuery,
threeitize,
}
15 changes: 0 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -117,19 +116,6 @@ const memoizeAsync = <T, A extends Array<any>>(method: (...args: A) => T) => {
/* eslint-enable functional/no-let */
}

const threeitize = <T>(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,
Expand All @@ -140,5 +126,4 @@ export {
flowFindFirst,
memoizeSync,
memoizeAsync,
threeitize,
}
36 changes: 36 additions & 0 deletions test/src/orm/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 0 additions & 36 deletions test/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading