Explorar o código

cow: just pass a list of keys/dotkeys to create.

Herbert Vojčík %!s(int64=7) %!d(string=hai) anos
pai
achega
04a9fe2906
Modificáronse 3 ficheiros con 26 adicións e 8 borrados
  1. 10 5
      README.md
  2. 9 2
      lib/cow-value-model.js
  3. 7 1
      src/cow-value-model.js

+ 10 - 5
README.md

@@ -60,11 +60,12 @@ Useful to "concatenate" a few `subReducer`s. like:
 	)
 ```
 
-## `cowValueModel(key)`
+## `cowValueModel(key, ...)`
 
 Creates an object with set of functions allowing
 to set or get specified key from any object.
 Get when one arg, set when two args.
+Specify keys by passing a list of keys to `cowValueModel`.
 You can use dot notation.
 
 Setting return a copy with specified (sub-)property changed;
@@ -79,25 +80,29 @@ in case no change actually happens, returns the original object.
     // => {name: "Jerry"}
     
     const city = cowValueModel("address.city");
+    const city2 = cowValueModel("address", "city");
     const object = {address: {city: "New York"}};
     
     city(object);
     // => "New York"
+    city2(object);
+    // => "New York"
     city(object, "London");
     // => {address: {city: "London"}}
+    city2(object, "London");
+    // => {address: {city: "London"}}
     object;
     // => {address: {city: "New York"}}
     city(object, "New York") === object;
     // => true
+    city2(object, "New York") === object;
+    // => true
 ```
 
-In case you pass second argument to `cowValueModel`,
-it is used as a parsed list of keys instead of default `firstArg.split('.')`.
-
 If you put a number in a list of keys to use,
 an object will be treated as an array (unlike the default string case,
 where it is treated as an object), so copy wil be created
 using `[...obj]`, not using `{...obj}`.
 
-That way you can create eg. `const c = cowValueObject("name of 34th person", ["person", 34, "name"])`
+That way you can create eg. `const c = cowValueObject("person", 34, "name")`
 to access `obj.person[34].name` with `c(obj)` / `c(obj, val)`.

+ 9 - 2
lib/cow-value-model.js

@@ -14,8 +14,15 @@ function copyWith(obj, key, value) {
     return result;
 }
 
-var cowValueModel = exports.cowValueModel = function cowValueModel(key) {
-    var keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : key.split('.');
+var cowValueModel = exports.cowValueModel = function cowValueModel() {
+    for (var _len = arguments.length, keyDescriptions = Array(_len), _key = 0; _key < _len; _key++) {
+        keyDescriptions[_key] = arguments[_key];
+    }
+
+    var keys = [];
+    keyDescriptions.forEach(function (each) {
+        if (typeof each === 'number') keys.push(each);else keys.push.apply(keys, _toConsumableArray(each.toString().split('.')));
+    });
 
     function setField() {
         var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

+ 7 - 1
src/cow-value-model.js

@@ -4,7 +4,13 @@ function copyWith (obj, key, value) {
     return result;
 }
 
-export const cowValueModel = (key, keys = key.split('.')) => {
+export const cowValueModel = (...keyDescriptions) => {
+    const keys = [];
+    keyDescriptions.forEach(each => {
+        if (typeof each === 'number') keys.push(each);
+        else keys.push(...each.toString().split('.'));
+    });
+
     function setField (x = {}, index, val) {
         if (index >= keys.length) return val;
         const key = keys[index],