From 2c4f9cf5c8ac3797b95dad7611200744abbc5fa4 Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:00:17 +0300 Subject: [PATCH 1/7] initial explanation --- proposal-struct-extration-and-applied-dest.md | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 proposal-struct-extration-and-applied-dest.md diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md new file mode 100644 index 0000000..5fd6d65 --- /dev/null +++ b/proposal-struct-extration-and-applied-dest.md @@ -0,0 +1,79 @@ +# ECMAScript proposal: structured partial object "shallow copy" and co-aplied destruction. +- [Motivation](#motivation) +- [High-level API](#high-level-api) + +## Motivation + +Fundamentally you need an existent object understand this proposal. From now being with ES6 at this pre-condition you can bring any structured [destruction to variables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). For example: + +```js +const myPreCondition = { + a : { + d : 1, + }, + b : { + e : 2, + f : 3 + }, + c : 2 +}; + +// then this creates two const : a and b +const {a, b} = myPreCondition; +``` + +And then, for example, we can assign this variables to the other existent object in a way like this: + +```js +const myOtherObject = {}; +// something like this +Object.assign(myOtherObject, {a, b} = ); +// let assume previous code as pre-condition +// so we have a and b as const at this moment +Object.assign(myOtherObject, {a, b}); +``` + + +So, the idea is about to extract only some necessary props to the other object directly, wich means without any intermediate additions. + + +## High-level API + +This declaration example we suppose `extra` will become an object as receive `a` and `c` from `myPreCondition` as properties: + +```js +const extra = myPreCondition.{a, c}; + +console.log(Object.keys(extra)); // ['a', 'c'] +console.log(extra); // { a : { d : 1 }, c : 2 } +``` + +This means we did `Object.assign` directly, without any intermediate memory consumption, just made key `a` in `extra` as pointer to `a` of `myPreCondition` and made key `c` as immediately destructured `2` as it was pointed to numeric vector value. + +One more example: + +```js +const extraFar = { + ...myPreCondition.{a, c}, + ...myPreCondition.b.{e}, +}; + +console.log(Object.keys(extraFar)); // ['a', 'c', 'e'] +``` + +## Assignment to an existent object + +```js + +const existentObject = { e : 1 }; + +existentObject = { + ...myPreCondition.{a, c}, + ...myPreCondition.b.{e}, +}; + +console.log(Object.keys(existentObject)); // ['a', 'c', 'e'] +console.log(existentObject.e); // 2, as it was at myPreCondition.b.e; +``` + +So, the behaviour is the same as it was `Object.assign`, but for sure can be optimized under the hood of V8. From 6eb16defbdcf47755bc4342a3ad1de7c48646ec5 Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:06:58 +0300 Subject: [PATCH 2/7] code fix --- proposal-struct-extration-and-applied-dest.md | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md index 5fd6d65..e581607 100644 --- a/proposal-struct-extration-and-applied-dest.md +++ b/proposal-struct-extration-and-applied-dest.md @@ -19,7 +19,7 @@ const myPreCondition = { }; // then this creates two const : a and b -const {a, b} = myPreCondition; +const { a, b } = myPreCondition; ``` And then, for example, we can assign this variables to the other existent object in a way like this: @@ -27,22 +27,23 @@ And then, for example, we can assign this variables to the other existent object ```js const myOtherObject = {}; // something like this -Object.assign(myOtherObject, {a, b} = ); -// let assume previous code as pre-condition +Object.assign(myOtherObject, { a, b } = myPreCondition); + +// or let assume previous code as pre-condition // so we have a and b as const at this moment -Object.assign(myOtherObject, {a, b}); +Object.assign(myOtherObject, { a, b }); ``` -So, the idea is about to extract only some necessary props to the other object directly, wich means without any intermediate additions. +So, the idea is about to extract only some necessary props to the other object directly, wich means without any intermediate actions. ## High-level API -This declaration example we suppose `extra` will become an object as receive `a` and `c` from `myPreCondition` as properties: +This example declaration we suppose `extra` will become an object as receive `a` and `c` from `myPreCondition` as properties: ```js -const extra = myPreCondition.{a, c}; +const extra = myPreCondition.{ a, c }; console.log(Object.keys(extra)); // ['a', 'c'] console.log(extra); // { a : { d : 1 }, c : 2 } @@ -50,15 +51,11 @@ console.log(extra); // { a : { d : 1 }, c : 2 } This means we did `Object.assign` directly, without any intermediate memory consumption, just made key `a` in `extra` as pointer to `a` of `myPreCondition` and made key `c` as immediately destructured `2` as it was pointed to numeric vector value. -One more example: +Partial deep extration example: ```js -const extraFar = { - ...myPreCondition.{a, c}, - ...myPreCondition.b.{e}, -}; - -console.log(Object.keys(extraFar)); // ['a', 'c', 'e'] +const deepExtra = myPreCondition.{ a, c, b.{e} }, +console.log(Object.keys(deepExtra)); // ['a', 'c', 'e'] ``` ## Assignment to an existent object @@ -67,10 +64,7 @@ console.log(Object.keys(extraFar)); // ['a', 'c', 'e'] const existentObject = { e : 1 }; -existentObject = { - ...myPreCondition.{a, c}, - ...myPreCondition.b.{e}, -}; +existentObject = myPreCondition.{ a, c, b.{e} }; console.log(Object.keys(existentObject)); // ['a', 'c', 'e'] console.log(existentObject.e); // 2, as it was at myPreCondition.b.e; From 4caa7bf3df13021d9058bc10fd9afbbde9ec90e6 Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:09:13 +0300 Subject: [PATCH 3/7] additional explanation --- proposal-struct-extration-and-applied-dest.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md index e581607..6002364 100644 --- a/proposal-struct-extration-and-applied-dest.md +++ b/proposal-struct-extration-and-applied-dest.md @@ -62,12 +62,13 @@ console.log(Object.keys(deepExtra)); // ['a', 'c', 'e'] ```js -const existentObject = { e : 1 }; +const existentObject = { e : 1, f : 2 }; -existentObject = myPreCondition.{ a, c, b.{e} }; +existentObject = myPreCondition.{ a, c, b.{e, f} }; -console.log(Object.keys(existentObject)); // ['a', 'c', 'e'] +console.log(Object.keys(existentObject)); // ['a', 'c', 'e', 'f'] console.log(existentObject.e); // 2, as it was at myPreCondition.b.e; +console.log(existentObject.f); // 3, as it was at myPreCondition.b.f; ``` So, the behaviour is the same as it was `Object.assign`, but for sure can be optimized under the hood of V8. From 28a870e445a2c6559f6a64f4e3f9e47d4f594283 Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:15:29 +0300 Subject: [PATCH 4/7] headers fix --- proposal-struct-extration-and-applied-dest.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md index 6002364..71a5406 100644 --- a/proposal-struct-extration-and-applied-dest.md +++ b/proposal-struct-extration-and-applied-dest.md @@ -1,6 +1,7 @@ -# ECMAScript proposal: structured partial object "shallow copy" and co-aplied destruction. +# ECMAScript proposal: structured partial object "shallow copy" and co-aplied destructuring. - [Motivation](#motivation) - [High-level API](#high-level-api) +- [Assignment to an existent object](#assignment-to-an-existent-object) ## Motivation From 3f6bf0b8c0d2a506fd921f73b7bb9c482c7652de Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:25:17 +0300 Subject: [PATCH 5/7] additions --- proposal-struct-extration-and-applied-dest.md | 1 + 1 file changed, 1 insertion(+) diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md index 71a5406..7778620 100644 --- a/proposal-struct-extration-and-applied-dest.md +++ b/proposal-struct-extration-and-applied-dest.md @@ -73,3 +73,4 @@ console.log(existentObject.f); // 3, as it was at myPreCondition.b.f; ``` So, the behaviour is the same as it was `Object.assign`, but for sure can be optimized under the hood of V8. +And in an addition we can throw an exception, if nested prop does not exists. But also we can bring some compatibility with other proposals with pre-cheking if property exists `obj?.deep?.nested` or `obj?deep?nested` or `obj.?deeo.?nested`, nevermind how it will be finally implemented. From 1845fadc4b57f4f6c9febe690c3289a469ef5f7a Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:39:33 +0300 Subject: [PATCH 6/7] spread extraction --- proposal-struct-extration-and-applied-dest.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md index 7778620..98a2b4b 100644 --- a/proposal-struct-extration-and-applied-dest.md +++ b/proposal-struct-extration-and-applied-dest.md @@ -14,7 +14,8 @@ const myPreCondition = { }, b : { e : 2, - f : 3 + f : 3, + g : 7 }, c : 2 }; @@ -41,7 +42,7 @@ So, the idea is about to extract only some necessary props to the other object d ## High-level API -This example declaration we suppose `extra` will become an object as receive `a` and `c` from `myPreCondition` as properties: +This example declaration with **extraction** we suppose `extra` will become an object as receive `a` and `c` from `myPreCondition` as properties: ```js const extra = myPreCondition.{ a, c }; @@ -52,13 +53,21 @@ console.log(extra); // { a : { d : 1 }, c : 2 } This means we did `Object.assign` directly, without any intermediate memory consumption, just made key `a` in `extra` as pointer to `a` of `myPreCondition` and made key `c` as immediately destructured `2` as it was pointed to numeric vector value. -Partial deep extration example: +Partial **deep extration** example: ```js const deepExtra = myPreCondition.{ a, c, b.{e} }, console.log(Object.keys(deepExtra)); // ['a', 'c', 'e'] ``` +Partial **spread extration** example: + +```js +const deepSpreadExtra = myPreCondition.{ a, b.{ ... } }, +console.log(Object.keys(deepExtra)); // ['a', 'e', 'f', 'g'] +console.log(deepExtra); // { a : { d : 1 }, e : 2, f : 3, g : 7 } +``` + ## Assignment to an existent object ```js From 38789ac8facf2ee6343eb558aaa127c5f68af96e Mon Sep 17 00:00:00 2001 From: went Date: Wed, 22 May 2019 23:46:03 +0300 Subject: [PATCH 7/7] fix 4 existent object explanation --- proposal-struct-extration-and-applied-dest.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/proposal-struct-extration-and-applied-dest.md b/proposal-struct-extration-and-applied-dest.md index 98a2b4b..62e84bb 100644 --- a/proposal-struct-extration-and-applied-dest.md +++ b/proposal-struct-extration-and-applied-dest.md @@ -72,11 +72,13 @@ console.log(deepExtra); // { a : { d : 1 }, e : 2, f : 3, g : 7 } ```js -const existentObject = { e : 1, f : 2 }; +const existentObject = { a: 8, e : 1, f : 2 }; -existentObject = myPreCondition.{ a, c, b.{e, f} }; +existentObject.{ ...myPreCondition.{ c, b.{e, f} } }; console.log(Object.keys(existentObject)); // ['a', 'c', 'e', 'f'] + +console.log(existentObject); // { a : 8, c : 2, e : 2, f : 3 } console.log(existentObject.e); // 2, as it was at myPreCondition.b.e; console.log(existentObject.f); // 3, as it was at myPreCondition.b.f; ```