From 4316d877c2b631e7d7da576719c97e3e55940b4d Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 9 Oct 2020 23:20:00 +0300 Subject: [PATCH] docs and minor tweaks... Signed-off-by: Alex A. Naanou --- Object.js | 28 ++++++++++++++++++++++++---- README.md | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Object.js b/Object.js index 7fba618..8696df5 100644 --- a/Object.js +++ b/Object.js @@ -51,14 +51,34 @@ toObject( //--------------------------------------------------------------------- +// Make a copy of an object... +// +// This will: +// - create a new object linked to the same prototype chain as obj +// - copy obj own state +// +// NOTE: this will copy prop values and not props... +Object.copy = function(obj, constructor){ + return Object.assign( + constructor == null ? + Object.create(obj.__proto__) + : constructor(), + obj) } + + // Make a full key set copy of an object... // +// NOTE: this will copy prop values and not props... // NOTE: this will not deep-copy the values... -Object.flatCopy = function(obj){ +Object.flatCopy = function(obj, constructor){ return Object.deepKeys(obj) - .reduce(function(res, key){ - res[key] = obj[key] - return res }, {}) } + .reduce( + function(res, key){ + res[key] = obj[key] + return res }, + constructor == null ? + {} + : constructor()) } // XXX for some reason neumric keys do not respect order... diff --git a/README.md b/README.md index 5b8710b..b36e692 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,10 @@ A library of JavaScript type extensions, types and type utilities. - [Built-in type extensions](#built-in-type-extensions) - [`Object`](#object) - [`Object.deepKeys(..)`](#objectdeepkeys) + - [`Object.copy(..)`](#objectcopy) + - [`Object.flatCopy(..)`](#objectflatcopy) - [`Object.match(..)`](#objectmatch) - [`Object.matchPartial(..)`](#objectmatchpartial) - - [`Object.flatCopy(..)`](#objectflatcopy) - [`.run(..)`](#objectrun) - [`Object.sort(..)`](#objectsort) - [`Array`](#array) @@ -76,7 +77,7 @@ To extend everything: require('ig-types') ``` -To have access to library types and utilities: +To have access to additional library types and utilities: ```javascript var types = require('ig-types') ``` @@ -84,20 +85,19 @@ var types = require('ig-types') `types.js` is organized so as to be able to import/extend only specific sub-modules mostly independently so... -In case there is a need to only extend a specific constructor: +In case there is a need to only extend a specific constructor just import +the module dealing with that constructor (`Array` in this case): ```javascript // require `ig-types/`... require('ig-types/Array') ``` +Note that type patching modules are mostly independent. And to import specific library modules only: ```javascript var containers = require('ig-types/containers') ``` -Note that though mostly independent now some sub-modules may import -others in the future. - ## Built-in type extensions @@ -105,12 +105,37 @@ others in the future. #### `Object.deepKeys(..)` +``` +Object.deepKeys() + -> +``` + +Get list of keys from all objects in the prototype chain. + +This is different from `Object.keys(..)` which only gets _own_ keys from the +current object. + +Example: +```javascript +var a = { x: 123 } +var b = Object.create(a) +b.y = 321 + +// get own keys of b... +Object.keys(b) // -> ['y'] + +// get all keys accessible from b... +Object.deepKeys(b) // -> ['x', 'y'] +``` + +#### `Object.copy(..)` + +#### `Object.flatCopy(..)` + #### `Object.match(..)` #### `Object.matchPartial(..)` -#### `Object.flatCopy(..)` - #### `.run(..)` ```