-
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.
- Loading branch information
Showing
11 changed files
with
170 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { getObjectTokenType } from '../tokens/main.js' | ||
|
||
// Normalize a path of tokens | ||
export const normalizePath = function (path) { | ||
return path.map(normalizeToken) | ||
} | ||
|
||
const normalizeToken = function (token) { | ||
return token | ||
const tokenType = getObjectTokenType(token) | ||
return tokenType.normalize(token) | ||
} |
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
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
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,92 @@ | ||
import isPlainObj from 'is-plain-obj' | ||
|
||
import { ARRAY_TOKEN, getArrayIndex, isIndexString } from './array.js' | ||
import { SLICE } from './special.js' | ||
|
||
// Check the type of a parsed token. | ||
const testObject = function (token) { | ||
return ( | ||
isPlainObj(token) && | ||
token.type === SLICE_TYPE && | ||
isEdge(token.from) && | ||
isEdge(token.to) | ||
) | ||
} | ||
|
||
const isEdge = function (edge) { | ||
return edge === undefined || ARRAY_TOKEN.testObject(edge) | ||
} | ||
|
||
// Serialize a token to a string | ||
const serialize = function ({ from, to }) { | ||
return `${serializeEdge(from)}${SLICE}${serializeEdge(to)}` | ||
} | ||
|
||
const serializeEdge = function (edge) { | ||
return edge === undefined ? DEFAULT_EDGE_STRING : ARRAY_TOKEN.serialize(edge) | ||
} | ||
|
||
// Check the type of a serialized token | ||
const testString = function ({ chars, hasSlice }) { | ||
return hasSlice && chars.split(SLICE).every(isEdgeString) | ||
} | ||
|
||
const isEdgeString = function (chars) { | ||
return chars === DEFAULT_EDGE_STRING || isIndexString(chars) | ||
} | ||
|
||
// Parse a string into a token | ||
const parse = function (chars) { | ||
const [from, to] = chars.split(SLICE).map(parseEdge) | ||
return { type: SLICE_TYPE, from, to } | ||
} | ||
|
||
const parseEdge = function (chars) { | ||
return chars === DEFAULT_EDGE_STRING ? undefined : ARRAY_TOKEN.parse(chars) | ||
} | ||
|
||
const DEFAULT_EDGE_STRING = '' | ||
const SLICE_TYPE = 'slice' | ||
|
||
// Normalize value after parsing or serializing | ||
const normalize = function ({ type, from = 0, to }) { | ||
const toA = Object.is(to, -0) ? undefined : to | ||
return { type, from, to: toA } | ||
} | ||
|
||
// When the token is missing a target value, add a default one. | ||
const isDefined = function (value) { | ||
return Array.isArray(value) | ||
} | ||
|
||
// Default value when token is missing | ||
const defaultValue = [] | ||
|
||
// Use the token to list entries against a target value. | ||
// eslint-disable-next-line max-params | ||
const getEntries = function (value, path, { from, to }, defined) { | ||
const fromIndex = getArrayIndex(value, from) | ||
const toIndex = Math.max(getArrayIndex(value, to), fromIndex) | ||
return new Array(toIndex - fromIndex).fill().map((_, index) => ({ | ||
value: value[index + fromIndex], | ||
path: [...path, index + fromIndex], | ||
defined, | ||
})) | ||
} | ||
|
||
// Check if two tokens are the same | ||
const equals = function (tokenA, tokenB) { | ||
return Object.is(tokenA.from, tokenB.from) && Object.is(tokenA.to, tokenB.to) | ||
} | ||
|
||
export const SLICE_TOKEN = { | ||
testObject, | ||
serialize, | ||
testString, | ||
parse, | ||
normalize, | ||
isDefined, | ||
defaultValue, | ||
getEntries, | ||
equals, | ||
} |
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