-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adb5d03
commit be2d3df
Showing
7 changed files
with
263 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import test from 'ava' | ||
|
||
import mapTransform = require('..') | ||
|
||
test('should map with object shape', (t) => { | ||
const def = { | ||
mapping: { | ||
attributes: { | ||
title: 'content.heading', | ||
text: 'content.copy' | ||
}, | ||
relationships: { | ||
author: 'meta.writer.username' | ||
} | ||
} | ||
} | ||
const data = { | ||
content: { heading: 'The heading', copy: 'A long text' }, | ||
meta: { writer: { username: 'johnf' } } | ||
} | ||
const expected = { | ||
attributes: { | ||
title: 'The heading', | ||
text: 'A long text' | ||
}, | ||
relationships: { | ||
author: 'johnf' | ||
} | ||
} | ||
|
||
const ret = mapTransform(def)(data) | ||
|
||
t.deepEqual(ret, expected) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import test from 'ava' | ||
|
||
import { normalizeMapping } from './normalizeMapping' | ||
|
||
test('should normalize to mapping objects', (t) => { | ||
const mapping = { | ||
title: { path: 'content.heading' }, | ||
author: { path: 'meta.writer.username' } | ||
} | ||
const expected = [ | ||
{ pathTo: 'title', path: 'content.heading' }, | ||
{ pathTo: 'author', path: 'meta.writer.username' } | ||
] | ||
|
||
const ret = normalizeMapping(mapping) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should normalize from path shortcut', (t) => { | ||
const mapping = { | ||
title: 'content.heading', | ||
author: 'meta.writer.username' | ||
} | ||
const expected = [ | ||
{ pathTo: 'title', path: 'content.heading' }, | ||
{ pathTo: 'author', path: 'meta.writer.username' } | ||
] | ||
|
||
const ret = normalizeMapping(mapping) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should normalize with null as shortcut', (t) => { | ||
const mapping = { | ||
title: null | ||
} | ||
const expected = [ | ||
{ pathTo: 'title', path: null } | ||
] | ||
|
||
const ret = normalizeMapping(mapping) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should normalize from object shape', (t) => { | ||
const mapping = { | ||
attributes: { | ||
title: 'content.heading', | ||
text: 'content.copy', | ||
deeper: { | ||
'with.path': 'id' | ||
} | ||
}, | ||
relationships: { | ||
author: 'meta.writer.username' | ||
} | ||
} | ||
const expected = [ | ||
{ pathTo: 'attributes.title', path: 'content.heading' }, | ||
{ pathTo: 'attributes.text', path: 'content.copy' }, | ||
{ pathTo: 'attributes.deeper.with.path', path: 'id' }, | ||
{ pathTo: 'relationships.author', path: 'meta.writer.username' } | ||
] | ||
|
||
const ret = normalizeMapping(mapping) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should normalize array of mapping objects', (t) => { | ||
const mapping = [ | ||
{ pathTo: 'title', path: 'content.heading' }, | ||
{ pathTo: 'author', path: 'meta.writer.username' } | ||
] | ||
const expected = [ | ||
{ pathTo: 'title', path: 'content.heading' }, | ||
{ pathTo: 'author', path: 'meta.writer.username' } | ||
] | ||
|
||
const ret = normalizeMapping(mapping) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should normalize mapping object', (t) => { | ||
const transform = (value: string) => value + ' norm' | ||
const transformRev = (value: string) => value + ' rev' | ||
const mapping = [ | ||
{ | ||
path: '', | ||
transform, | ||
transformRev, | ||
default: 'Untitled', | ||
defaultRev: 'Titled after all' | ||
} | ||
] | ||
const expected = [ | ||
{ | ||
pathTo: null, | ||
path: null, | ||
transform, | ||
transformRev, | ||
default: 'Untitled', | ||
defaultRev: 'Titled after all' | ||
} | ||
] | ||
|
||
const ret = normalizeMapping(mapping) | ||
|
||
t.deepEqual(ret, expected) | ||
}) |
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,47 @@ | ||
import { Shape } from '..' | ||
import { PathString } from './lensPath' | ||
import { TransformPipeline } from './transformPipeline' | ||
|
||
interface MappingDefBase { | ||
path: PathString | null, | ||
transform?: TransformPipeline, | ||
transformRev?: TransformPipeline, | ||
default?: any, | ||
defaultRev?: any | ||
} | ||
|
||
export interface MappingDefNormalized extends MappingDefBase { | ||
pathTo: PathString | null, | ||
} | ||
|
||
export interface MappingDef extends MappingDefBase { | ||
pathTo?: PathString | null, | ||
} | ||
|
||
const normalize = (def: MappingDef): MappingDefNormalized => | ||
({ ...def, path: def.path || null, pathTo: def.pathTo || null }) | ||
|
||
const normalizeDef = (pathArr: PathString[], def: MappingDef | PathString): | ||
MappingDefNormalized => | ||
(typeof def === 'string') | ||
? normalize({ path: def, pathTo: pathArr.join('.') }) | ||
: normalize({ ...def, pathTo: pathArr.join('.') }) | ||
|
||
const isMappingDef = (obj: Shape | PathString | MappingDef | null) => | ||
obj && typeof obj === 'object' && typeof (obj as MappingDef).path !== 'string' | ||
|
||
const normalizeShape = (mapping: Shape, pathTo: string[] = []): | ||
MappingDefNormalized[] => | ||
Object.keys(mapping).reduce((arr: MappingDefNormalized[], key: PathString) => | ||
(isMappingDef(mapping[key])) | ||
? [ ...arr, ...normalizeShape(mapping[key] as Shape, [...pathTo, key]) ] | ||
: [ ...arr, normalizeDef([...pathTo, key], mapping[key] as MappingDef)] | ||
, [] | ||
) | ||
|
||
export function normalizeMapping (mapping: Shape | MappingDef[]): | ||
MappingDefNormalized[] { | ||
return (Array.isArray(mapping)) | ||
? mapping.map(normalize) | ||
: normalizeShape(mapping) | ||
} |