Skip to content

Commit

Permalink
Make null explicit with IPath
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Jun 11, 2018
1 parent 8683cd2 commit 587c806
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// Definitions file for map-transform
declare function mapTransform (mapping: object): (data: object) => object

export type IPath = string | null
export type IPath = string

export interface IFieldMapping {
path: IPath,
path: IPath | null,
default?: any,
defaultRev?: any
}

export interface IMapping {
fields?: {
[key: string]: IPath | IFieldMapping
[key: string]: IPath | IFieldMapping | null
},
path?: IPath,
pathRev?: IPath,
pathTo?: IPath,
pathToRev?: IPath
path?: IPath | null,
pathRev?: IPath | null,
pathTo?: IPath | null,
pathToRev?: IPath | null
}

type IDataProperty = string | number | object
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "map-transform",
"version": "0.1.1",
"version": "0.1.2",
"description": "Map and transform objects with mapping definitions",
"main": "index.js",
"types": "index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/createFieldMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as R from 'ramda'
import { IFieldMapping, IFieldMapper, IPath } from '../../index.d'
import lensPath from './lensPath'

type IFieldMappingTuple = [string, IPath | IFieldMapping]
type IFieldMappingTuple = [string, IPath | IFieldMapping | null]
type IFieldMapperGetter = (isRev: boolean) => IFieldMapper

// String | b -> b
const normalizeFieldMapping = (fieldMapping: IPath | IFieldMapping): IFieldMapping =>
const normalizeFieldMapping = (fieldMapping: IPath | IFieldMapping | null): IFieldMapping =>
(!fieldMapping || typeof fieldMapping === 'string')
? { path: fieldMapping }
: fieldMapping
Expand Down
7 changes: 3 additions & 4 deletions src/lib/lensPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const parseIntIf = (val: string) => {
}

// String -> (String | Number)[]
const preparePath = (path: IPath): (string | number)[] => (path)
? path.split(/\[|]?\./).map((val: string) => parseIntIf(val))
: []
const preparePath = (path: IPath): (string | number)[] =>
path.split(/\[|]?\./).map((val: string) => parseIntIf(val))

/**
* Take a path string and return a Ramda lens. If path is null or undefined, an
Expand All @@ -25,6 +24,6 @@ const preparePath = (path: IPath): (string | number)[] => (path)
* @param {string} path - A path string in dot notation
* @returns {Lens} A Ramda lens
*/
const lensPath = (path?: IPath): R.Lens => (path) ? R.lensPath(preparePath(path)) : empty
const lensPath = (path?: IPath | null): R.Lens => (path) ? R.lensPath(preparePath(path)) : empty

export default lensPath

0 comments on commit 587c806

Please sign in to comment.