Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/checkField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import predicate from "predicate";
import predicate from "predicate/dist/predicate";
import { isObject } from "./utils";

import { AND, NOT, OR } from "./constants";
Expand Down
4 changes: 2 additions & 2 deletions src/conditionsMeet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
43 changes: 43 additions & 0 deletions src/selectn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

var curry2 = require("curry2");
var dotted = require("brackets2dots");
var splits = require("dotsplit.js");

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 = splits(dotted(path));
var end = seg.length;

if (!end) {
return undefined;
}

var ref = object;

while (++idx < end) {
if (Object(ref) !== ref) {
return undefined;
}
ref = ref[seg[idx]];
}

return typeof ref === "function" ? ref() : ref;
}
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import selectn from "selectn";
import selectn from "./selectn";

export function normRef(ref) {
return ref.replace(/\$/g, ".");
Expand Down
2 changes: 1 addition & 1 deletion src/validation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import predicate from "predicate";
import predicate from "predicate/dist/predicate";
import {
flatMap,
isObject,
Expand Down
2 changes: 1 addition & 1 deletion test/predicate.test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
16 changes: 5 additions & 11 deletions test/selectn.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import selectn from "selectn";
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);
});
5 changes: 4 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/validation.predicates.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import predicate from "predicate";
import predicate from "predicate/dist/predicate";
import { listInvalidPredicates } from "../src/validation";

let schema = {
Expand Down