0.0.24 • Published 3 years ago

@browndragon/destructure v0.0.24

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

@browndragon/destructure

Install with $ npm i @browndragon/destructure and import as normal with

import destructure from `@browndragon/destructure`
let [prefix, last] = destructure(longerArray); 

Why Destructure?

es6 gives us the new destructuring syntax:

let [a, b, ...rest] = [...'abcdef'];

But what if you wanted the righthand side, as with something like:

:warning: This doesn't actually work!

function recursivelySet(object, ...path, key, value) {
    for (let p of path) {
        if (!object) {
            return undefined;
        }
        object = object[path];
    }
    return object[key] = value;
}

Well, you still can't write that, but you can write:

function recursivelySet(object, ...pathKeyValue) {
    let [path, key, value] = destructure(pathKeyValue, 2);
    // Same body as before.
}

The default number of right parameters is 1, so if you're just popping one element you don't need to pass anything (const [path, last] = destructure(pathAndLast)). You can also destructure some left hand parameters at the same time for potential efficiency gains: const [first, middleArray, last] = destructure(array, 1, 1).

0.0.23

3 years ago

0.0.24

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago