From d7ec9f2446bf424a7ee8504a7cf8efb2de5e104d Mon Sep 17 00:00:00 2001 From: Rauno Moisto Date: Thu, 5 Sep 2019 17:11:05 +0300 Subject: [PATCH 1/3] make it work on older browsers --- package.json | 6 ++++-- src/checkField.js | 2 +- src/selectn.js | 40 ++++++++++++++++++++++++++++++++++++++++ src/utils.js | 2 +- src/validation.js | 2 +- 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/selectn.js diff --git a/package.json b/package.json index ff087eb..389d0fa 100644 --- a/package.json +++ b/package.json @@ -42,9 +42,11 @@ "node": ">=8" }, "dependencies": { + "brackets2dots": "^1.1.0", + "curry2": "^1.0.3", "deep-equal": "1.0.1", - "predicate": "1.2.0", - "selectn": "1.1.2" + "dotsplit.js": "^1.1.0", + "predicate": "1.2.0" }, "devDependencies": { "atob": "^2.0.3", diff --git a/src/checkField.js b/src/checkField.js index 8df545c..0661513 100644 --- a/src/checkField.js +++ b/src/checkField.js @@ -1,4 +1,4 @@ -import predicate from "predicate"; +import predicate from "predicate/dist/predicate"; import { isObject } from "./utils"; import { AND, NOT, OR } from "./constants"; diff --git a/src/selectn.js b/src/selectn.js new file mode 100644 index 0000000..f994bf0 --- /dev/null +++ b/src/selectn.js @@ -0,0 +1,40 @@ +"use strict"; + +var curry2 = require("curry2"); +var dotted = require("brackets2dots"); +var splits = require("dotsplit.js"); +var string = Object.prototype.toString; + +module.exports = curry2(selectn); + +/** + * Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation + * string path while mitigating `TypeErrors` via friendly and composable API. + * + * @param {String|Array} path + * Dot/bracket-notation string path or array. + * + * @param {Object} object + * Object to access. + * + * @return {Function|*|undefined} + * (1) returns `selectn/1` when partially applied. + * (2) returns value at path if path exists. + * (3) returns undefined if path does not exist. + */ +function selectn(path, object) { + var idx = -1; + var seg = + string.call(path) === "[object Array]" ? path : splits(dotted(path)); + var end = seg.length; + var ref = end ? object : void 0; + + while (++idx < end) { + if (Object(ref) !== ref) { + return void 0; + } + ref = ref[seg[idx]]; + } + + return typeof ref === "function" ? ref() : ref; +} diff --git a/src/utils.js b/src/utils.js index 69c8bd2..df4dede 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ -import selectn from "selectn"; +import selectn from "./selectn"; export function normRef(ref) { return ref.replace(/\$/g, "."); diff --git a/src/validation.js b/src/validation.js index 21a54b1..245212a 100644 --- a/src/validation.js +++ b/src/validation.js @@ -1,4 +1,4 @@ -import predicate from "predicate"; +import predicate from "predicate/dist/predicate"; import { flatMap, isObject, From 6ed26140028d1755ce86546dc28198e313eb4b0e Mon Sep 17 00:00:00 2001 From: Rauno Moisto Date: Thu, 5 Sep 2019 17:16:28 +0300 Subject: [PATCH 2/3] fixed tests --- src/conditionsMeet.js | 4 ++-- test/predicate.test.js | 2 +- test/selectn.test.js | 2 +- test/utils.test.js | 5 ++++- test/validation.predicates.test.js | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/conditionsMeet.js b/src/conditionsMeet.js index f1011a4..5b0e4ec 100644 --- a/src/conditionsMeet.js +++ b/src/conditionsMeet.js @@ -35,8 +35,8 @@ export default function conditionsMeet(condition, formData) { } else { let refVal = selectRef(ref, formData); if (Array.isArray(refVal)) { - let condMeatOnce = refVal.some( - val => (isObject(val) ? conditionsMeet(refCondition, val) : false) + let condMeatOnce = refVal.some(val => + isObject(val) ? conditionsMeet(refCondition, val) : false ); // It's either true for an element in an array or for the whole array return ( diff --git a/test/predicate.test.js b/test/predicate.test.js index 8f92031..1b8e82d 100644 --- a/test/predicate.test.js +++ b/test/predicate.test.js @@ -1,4 +1,4 @@ -import predicate from "predicate"; +import predicate from "predicate/dist/predicate"; import Engine from "../src/Engine"; test("equal work with same strings", function() { diff --git a/test/selectn.test.js b/test/selectn.test.js index 7313b76..c8ce846 100644 --- a/test/selectn.test.js +++ b/test/selectn.test.js @@ -1,4 +1,4 @@ -import selectn from "selectn"; +import selectn from "../src/selectn"; test("selectn on array", function() { let a = { diff --git a/test/utils.test.js b/test/utils.test.js index daacd90..da6c8f6 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -72,7 +72,10 @@ test("extract referenced schema", () => { }, }; - let { definitions: { medication }, properties: { registration } } = schema; + let { + definitions: { medication }, + properties: { registration }, + } = schema; expect(isRefArray("medications", schema)).toBeTruthy(); expect(extractRefSchema("medications", schema)).toEqual(medication); diff --git a/test/validation.predicates.test.js b/test/validation.predicates.test.js index fa72d92..79aa0fd 100644 --- a/test/validation.predicates.test.js +++ b/test/validation.predicates.test.js @@ -1,4 +1,4 @@ -import predicate from "predicate"; +import predicate from "predicate/dist/predicate"; import { listInvalidPredicates } from "../src/validation"; let schema = { From e5e4f5e0b3b67a7281b442407e864f838c0120c9 Mon Sep 17 00:00:00 2001 From: Rauno Moisto Date: Fri, 6 Sep 2019 10:24:58 +0300 Subject: [PATCH 3/3] improved test coverage --- src/selectn.js | 13 ++++++++----- test/selectn.test.js | 14 ++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/selectn.js b/src/selectn.js index f994bf0..7f2832b 100644 --- a/src/selectn.js +++ b/src/selectn.js @@ -3,7 +3,6 @@ var curry2 = require("curry2"); var dotted = require("brackets2dots"); var splits = require("dotsplit.js"); -var string = Object.prototype.toString; module.exports = curry2(selectn); @@ -24,14 +23,18 @@ module.exports = curry2(selectn); */ function selectn(path, object) { var idx = -1; - var seg = - string.call(path) === "[object Array]" ? path : splits(dotted(path)); + var seg = splits(dotted(path)); var end = seg.length; - var ref = end ? object : void 0; + + if (!end) { + return undefined; + } + + var ref = object; while (++idx < end) { if (Object(ref) !== ref) { - return void 0; + return undefined; } ref = ref[seg[idx]]; } diff --git a/test/selectn.test.js b/test/selectn.test.js index c8ce846..c166ec4 100644 --- a/test/selectn.test.js +++ b/test/selectn.test.js @@ -1,20 +1,14 @@ import selectn from "../src/selectn"; -test("selectn on array", function() { +test("selectn", function() { let a = { medications: { type: "A", + fn: () => "B", }, }; expect(selectn("medications.type", a)).toEqual("A"); - - // let obj = { - // medications: [ - // { type: "A" }, - // { type: "B" }, - // { type: "C" } - // ] - // }; - //expect(selectn("medications.type", obj)).toEqual(["A", "B", "C"]); + expect(selectn("medications.fn", a)).toEqual("B"); + expect(selectn("", a)).toEqual(undefined); });