camel-dot-prop-immutable
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -82,3 +82,3 @@ (function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap | ||
function get(obj, prop, value) { | ||
let match = getCamel({ obj, prop }); | ||
let match = camelDotMatch({ obj, prop }); | ||
return match.result || value; | ||
@@ -88,3 +88,3 @@ } | ||
function set(obj, prop, value) { | ||
let match = getCamel({ obj, prop }); | ||
let match = camelDotMatch({ obj, prop }); | ||
return _dotPropImmutable2.default.set(obj, match.prop, value); | ||
@@ -94,3 +94,3 @@ } | ||
function _delete(obj, prop) { | ||
let match = getCamel({ obj, prop }); | ||
let match = camelDotMatch({ obj, prop }); | ||
return _dotPropImmutable2.default.delete(obj, match.prop); | ||
@@ -100,3 +100,3 @@ } | ||
function toggle(obj, prop) { | ||
let match = getCamel({ obj, prop }); | ||
let match = camelDotMatch({ obj, prop }); | ||
return _dotPropImmutable2.default.toggle(obj, match.prop); | ||
@@ -106,10 +106,20 @@ } | ||
function merge(obj, prop, value) { | ||
let match = getCamel({ obj, prop }); | ||
let match = camelDotMatch({ obj, prop }); | ||
return _dotPropImmutable2.default.merge(obj, match.prop, value); | ||
} | ||
function getCamel({ obj, prop }) { | ||
let ogProp = prop; | ||
function camelDotMatch({ obj, prop }) { | ||
return tryCombos({ dir: "right", obj, prop }) || tryCombos({ dir: "left", obj, prop }) || { prop }; | ||
} | ||
function tryCombos({ obj, prop, dir = "left" }) { | ||
let result = _dotPropImmutable2.default.get(obj, prop); | ||
let regex; | ||
if (dir == "left") { | ||
regex = /\.\w/; | ||
} else { | ||
regex = /\.[^.]+$/; | ||
} | ||
if (!result && prop.constructor === String) { | ||
@@ -119,3 +129,3 @@ let lastProp; | ||
lastProp = prop; | ||
prop = prop.replace(/\.\w/, match => match.charAt(1).toUpperCase()); | ||
prop = prop.replace(regex, match => match.charAt(1).toUpperCase() + match.slice(2)); | ||
result = _dotPropImmutable2.default.get(obj, prop); | ||
@@ -125,10 +135,9 @@ } while (!result && lastProp != prop); | ||
if (result === undefined) { | ||
prop = ogProp; | ||
if (result !== undefined) { | ||
return { prop, result }; | ||
} | ||
return { prop, result }; | ||
} | ||
module.exports = _extends({}, _dotPropImmutable2.default, { | ||
camelDotMatch, | ||
delete: _delete, | ||
@@ -135,0 +144,0 @@ get, |
import dot from "dot-prop-immutable" | ||
function get(obj, prop, value) { | ||
let match = getCamel({ obj, prop }) | ||
let match = camelDotMatch({ obj, prop }) | ||
return match.result || value | ||
@@ -9,3 +9,3 @@ } | ||
function set(obj, prop, value) { | ||
let match = getCamel({ obj, prop }) | ||
let match = camelDotMatch({ obj, prop }) | ||
return dot.set(obj, match.prop, value) | ||
@@ -15,3 +15,3 @@ } | ||
function _delete(obj, prop) { | ||
let match = getCamel({ obj, prop }) | ||
let match = camelDotMatch({ obj, prop }) | ||
return dot.delete(obj, match.prop) | ||
@@ -21,3 +21,3 @@ } | ||
function toggle(obj, prop) { | ||
let match = getCamel({ obj, prop }) | ||
let match = camelDotMatch({ obj, prop }) | ||
return dot.toggle(obj, match.prop) | ||
@@ -27,10 +27,23 @@ } | ||
function merge(obj, prop, value) { | ||
let match = getCamel({ obj, prop }) | ||
let match = camelDotMatch({ obj, prop }) | ||
return dot.merge(obj, match.prop, value) | ||
} | ||
function getCamel({ obj, prop }) { | ||
let ogProp = prop | ||
function camelDotMatch({ obj, prop }) { | ||
return ( | ||
tryCombos({ dir: "right", obj, prop }) || | ||
tryCombos({ dir: "left", obj, prop }) || { prop } | ||
) | ||
} | ||
function tryCombos({ obj, prop, dir = "left" }) { | ||
let result = dot.get(obj, prop) | ||
let regex | ||
if (dir == "left") { | ||
regex = /\.\w/ | ||
} else { | ||
regex = /\.[^.]+$/ | ||
} | ||
if (!result && prop.constructor === String) { | ||
@@ -40,4 +53,6 @@ let lastProp | ||
lastProp = prop | ||
prop = prop.replace(/\.\w/, match => | ||
match.charAt(1).toUpperCase() | ||
prop = prop.replace( | ||
regex, | ||
match => | ||
match.charAt(1).toUpperCase() + match.slice(2) | ||
) | ||
@@ -48,7 +63,5 @@ result = dot.get(obj, prop) | ||
if (result === undefined) { | ||
prop = ogProp | ||
if (result !== undefined) { | ||
return { prop, result } | ||
} | ||
return { prop, result } | ||
} | ||
@@ -58,2 +71,3 @@ | ||
...dot, | ||
camelDotMatch, | ||
delete: _delete, | ||
@@ -60,0 +74,0 @@ get, |
{ | ||
"name": "camel-dot-prop-immutable", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": | ||
@@ -5,0 +5,0 @@ "dot-prop-immutable w/ check for flattened props", |
# camel-dot-prop-immutable | ||
This extension to [dot-prop-immutable](/debitoor/dot-prop-immutable) allows `foo.bar` to match `{ foo: bar: 1 }` and `{ fooBar: 1 }`. | ||
This extension to [dot-prop-immutable](/debitoor/dot-prop-immutable) allows `foo.bar.buzz` to match `{ foo: { barBuzz } }` and `{ fooBar: { buzz } }`. | ||
@@ -17,4 +17,26 @@ ```js | ||
dot.get({ foo: { barBuzz: "unicorn" } }, "foo.bar.buzz") | ||
//=> 'unicorn' | ||
dot.get({ fooBarBuzz: "unicorn" }, "foo.bar.buzz") | ||
//=> 'unicorn' | ||
``` | ||
## Restrictions | ||
Only collapse from left or right. Middle collapses like this will not work: | ||
```js | ||
dot.get({ foo: { barBuzz: { bang: "unicorn" } }, "foo.bar.buzz.bang") | ||
//=> 'unicorn' | ||
``` | ||
But these do: | ||
```js | ||
dot.get({ fooBar: { buzz: { bang: "unicorn" } }, "foo.bar.buzz.bang") | ||
//=> 'unicorn' | ||
dot.get({ foo: { bar: { buzzBang: "unicorn" } }, "foo.bar.buzz.bang") | ||
//=> 'unicorn' | ||
``` |
@@ -18,2 +18,6 @@ import dot from "../lib" | ||
).toBe("unicorn") | ||
expect( | ||
dot.get({ foo: { barBuzz: "unicorn" } }, "foo.bar.buzz") | ||
).toBe("unicorn") | ||
}) | ||
@@ -33,2 +37,8 @@ | ||
}) | ||
expect( | ||
dot.set({ foo: { barBuzz: "a" } }, "foo.bar.buzz", "b") | ||
).toEqual({ | ||
foo: { barBuzz: "b" }, | ||
}) | ||
}) | ||
@@ -42,2 +52,6 @@ | ||
expect(dot.delete({ fooBar: "a" }, "foo.bar")).toEqual({}) | ||
expect( | ||
dot.delete({ foo: { barBuzz: "a" } }, "foo.bar.buzz") | ||
).toEqual({ foo: {} }) | ||
}) | ||
@@ -53,2 +67,6 @@ | ||
}) | ||
expect( | ||
dot.toggle({ foo: { barBuzz: true } }, "foo.bar.buzz") | ||
).toEqual({ foo: { barBuzz: false } }) | ||
}) | ||
@@ -68,2 +86,12 @@ | ||
).toEqual({ fooBar: { bang: "a", buzz: "b" } }) | ||
expect( | ||
dot.merge( | ||
{ foo: { barBang: { buzz: "a" } } }, | ||
"foo.bar.bang", | ||
{ | ||
buzz: "b", | ||
} | ||
) | ||
).toEqual({ foo: { barBang: { buzz: "b" } } }) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11970
326
42