@finnair/v-validation-core
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -6,4 +6,12 @@ # Change Log | ||
# [0.2.0](https://github.com/finnair/v-validation/compare/v0.1.4...v0.2.0) (2020-04-15) | ||
**Note:** Version bump only for package @finnair/v-validation-core | ||
## [0.1.4](https://github.com/finnair/v-validation/compare/v0.1.3...v0.1.4) (2020-04-01) | ||
**Note:** Version bump only for package @finnair/v-validation-core |
@@ -9,5 +9,9 @@ export declare type PathComponent = number | string; | ||
toJSON(): string; | ||
get length(): number; | ||
get(index: number): PathComponent; | ||
[Symbol.iterator](): IterableIterator<PathComponent>; | ||
unset(object: any): any; | ||
set(object: any, value: any): any; | ||
static newRoot(): Path; | ||
static of(...path: PathComponent[]): Path; | ||
static ofNodes(path: PathComponent[]): Path; | ||
} | ||
@@ -14,0 +18,0 @@ export declare const ROOT: Path; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const identifier = /^[a-zA-Z_]+[a-zA-Z0-9_]*$/; | ||
const ROOT_ID = '$'; | ||
const identifier = /^[a-zA-Z_][a-zA-Z0-9_]*$/; | ||
class Path { | ||
@@ -16,21 +15,65 @@ constructor(path) { | ||
connectTo(newRootPath) { | ||
return new Path(newRootPath.path.concat(this.path.slice(1))); | ||
return new Path(newRootPath.path.concat(this.path)); | ||
} | ||
toJSON() { | ||
return this.path.slice(1).reduce((pathString, component) => pathString + componentToString(component), ROOT_ID); | ||
return this.path.reduce((pathString, component) => pathString + componentToString(component), '$'); | ||
} | ||
get length() { | ||
return this.path.length; | ||
} | ||
get(index) { | ||
return this.path[index]; | ||
} | ||
[Symbol.iterator]() { | ||
return this.path[Symbol.iterator](); | ||
} | ||
unset(object) { | ||
return this.set(object, undefined); | ||
} | ||
set(object, value) { | ||
if (this.path.length === 0) { | ||
return value; | ||
} | ||
let index = -1; | ||
const root = toObject(object, this.path); | ||
let current = root; | ||
for (index = 0; index < this.path.length - 1 && current; index++) { | ||
const component = this.path[index]; | ||
const child = toObject(current[component], this.path); | ||
current[component] = child; | ||
current = child; | ||
} | ||
if (value === undefined) { | ||
if (current !== undefined) { | ||
delete current[this.path[index]]; | ||
} | ||
} | ||
else { | ||
current[this.path[index]] = value; | ||
} | ||
return root; | ||
function toObject(current, path) { | ||
if (typeof current === 'object') { | ||
return current; | ||
} | ||
else if (value !== undefined) { | ||
if (typeof path[index + 1] === 'number') { | ||
return []; | ||
} | ||
else { | ||
return {}; | ||
} | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
} | ||
static newRoot() { | ||
return new Path([ROOT_ID]); | ||
return new Path([]); | ||
} | ||
static of(...path) { | ||
return Path.ofNodes(path); | ||
} | ||
static ofNodes(path) { | ||
if (path.length === 0) { | ||
return exports.ROOT; | ||
} | ||
if (path[0] !== ROOT_ID) { | ||
const normalizedPath = [ROOT_ID]; | ||
return new Path(normalizedPath.concat(path)); | ||
} | ||
return new Path(path); | ||
@@ -37,0 +80,0 @@ } |
{ | ||
"name": "@finnair/v-validation-core", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "V-validation core package", | ||
@@ -15,2 +15,9 @@ "main": "dist/index.js", | ||
}, | ||
"keywords": [ | ||
"validation", | ||
"validate", | ||
"convert", | ||
"normalize", | ||
"typescript" | ||
], | ||
"scripts": { | ||
@@ -25,3 +32,3 @@ "build": "tsc -b ." | ||
}, | ||
"gitHead": "baf1f28b41f67421c5e38825d5e050121883771f" | ||
"gitHead": "8e291a534a701538a8fb8a363ecbd5fddcd0ff95" | ||
} |
85336
2101