-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(listDocuments): use transducer and beef up tests #36
- Loading branch information
1 parent
980c55d
commit 705d9a8
Showing
4 changed files
with
175 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export { assertEquals, assertObjectMatch } from 'https://deno.land/[email protected]/testing/asserts.ts' | ||
export { | ||
assert, | ||
assertEquals, | ||
assertObjectMatch, | ||
} from 'https://deno.land/[email protected]/testing/asserts.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { R } from './deps.js' | ||
|
||
const { | ||
allPass, | ||
isNil, | ||
complement, | ||
transduce, | ||
append, | ||
omit, | ||
pluck, | ||
map, | ||
filter, | ||
compose, | ||
} = R | ||
|
||
export const isDefined = complement(isNil) | ||
|
||
export const isDesignDoc = (doc) => (/^_design/.test(doc._id)) | ||
export const isNotDesignDoc = complement(isDesignDoc) | ||
|
||
export const pluckDoc = pluck('doc') | ||
export const omitRev = omit(['rev', '_rev']) | ||
|
||
/** | ||
* A transduce allows us to iterate the array only once, | ||
* performing composed transformations inlined with reducing, | ||
* -- hence "trans"-"duce". | ||
* | ||
* This prevents iterating the array multiple times to perform multiple | ||
* transformations | ||
* | ||
* NOTE: compositions passed to transduce run top -> bottom instead of the usual | ||
* bottom to top. This is becase we are composing transformers which are functions | ||
* not values | ||
*/ | ||
export const foldWith = (transformer) => (iter) => | ||
transduce(transformer, (acc, item) => append(item, acc), [], iter) | ||
|
||
export const sanitizeDocs = foldWith( | ||
compose( | ||
filter(allPass([isDefined, isNotDesignDoc])), | ||
map(omitRev), | ||
), | ||
) | ||
|
||
/** | ||
* Each row is like | ||
* { | ||
key: '1', | ||
id: '1', | ||
value: { rev: '1' }, | ||
doc: { _id: '1', _rev: '1', hello: 'world' }, | ||
} | ||
So pluck the doc first to pass into sanitizing | ||
*/ | ||
export const sanitizeRows = foldWith( | ||
compose( | ||
pluck('doc'), | ||
filter(allPass([isDefined, isNotDesignDoc])), | ||
map(omitRev), | ||
), | ||
) |