More Help to use this
Closed this issue ยท 1 comments
HI, im new in the node.js field and dont know how to use this one.
can you upload step by step guide for newbies.
no problem if you feel hesitate by my answer. i know that not an issue request.
Hey there, welcome!
So after ensuring that Node.js is installed, you have access to npm
too which allows you to download & require packages from the registry. This is where klona
is available too.
If you already have a project with a package.json
within it, you can cd
into that file's directory. Otherwise, you can npm init -y
into a directory of your choice (generally the project root) which will scaffold a basic package.json
file in that directory. This file basically tracks your project's dependencies and their versions.
So now you can npm install klona
. This will download it and attach it to the dependencies
group within your (new?) package.json
file. From here, you can start using it!
Here's a little script that you can paste into a new file. It illustrates some of what klona
does at the most basic level:
// index.js
const klona = require('klona');
function data() {
return {
foo: 1,
bar: {
baz: 2,
bat: {
hello: 'world'
}
}
};
}
function tests(input, output) {
output.hello = 'world'; // "shallow" update
output.bar.foobar = 12345; // "deep" update
console.log('Our new object', JSON.stringify(output));
console.log('~> (input.hello) should be undefined:', input.hello);
console.log('~> (input.bar.foobar) should be undefined:', input.bar.foobar);
}
console.log('\n# Object.assign');
const input1 = data();
const output1 = Object.assign({}, input1);
tests(input1, output1);
console.log('\n# { ...spread }');
const input2 = data();
const output2 = { ...input2 };
tests(input2, output2);
console.log('\n# klona');
const input3 = data();
const output3 = klona(input3);
tests(input3, output3);
Now you can run this file to see the differences:
$ node index.js
Note: I'm assuming you called your file
index.js
here.
You'll see here that the two native APIs allowed a nested assignment to accidentally "leak" into the input object โ klona
prevents this, along with some other datatype-specific protections too.
Hope that helps! And good luck on your adventures with Node ๐