Skip to content

Commit 0c99e67

Browse files
committed
feat: initial commit
1 parent bdf650f commit 0c99e67

File tree

5 files changed

+94
-38
lines changed

5 files changed

+94
-38
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ yarn add pick-original
3737
## Usage
3838

3939
```js
40-
const PickOriginal = require('pick-original');
40+
const pickOriginal = require('pick-original');
4141

42-
const pickOriginal = new PickOriginal();
42+
function createObj(doc) {
43+
doc.id = Date.now().toString();
44+
return doc;
45+
}
4346

44-
console.log(pickOriginal.renderName());
45-
// script
47+
const original = { foo: 'bar' };
48+
49+
const transformed = pickOriginal(createObj(original), original);
50+
51+
console.log(transformed);
52+
53+
// { foo: 'bar' }
4654
```
4755

4856

index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
class Script {
2-
constructor(config) {
3-
config = { ...config };
4-
this._name = config.name || 'script';
1+
const _ = require('lodash');
2+
const dotify = require('node-dotify');
53

6-
this.renderName = this.renderName.bind(this);
4+
const pickOriginal = function(transformed, original) {
5+
const obj = {};
6+
for (const key of Object.keys(dotify(original))) {
7+
const value = _.get(transformed, key);
8+
if (!_.isUndefined(value)) _.set(obj, key, value);
79
}
810

9-
renderName() {
10-
return this._name;
11-
}
12-
}
11+
return obj;
12+
};
1313

14-
module.exports = Script;
14+
module.exports = pickOriginal;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"contributors": [
1616
"Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)"
1717
],
18-
"dependencies": {},
18+
"dependencies": {
19+
"lodash": "^4.17.15",
20+
"node-dotify": "^1.0.1"
21+
},
1922
"devDependencies": {
2023
"@commitlint/cli": "latest",
2124
"@commitlint/config-conventional": "latest",

test/test.js

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,67 @@
11
const test = require('ava');
22

3-
const Script = require('..');
3+
const pickOriginal = require('..');
44

5-
test.beforeEach(t => {
6-
const script = new Script({});
7-
Object.assign(t.context, { script });
8-
});
9-
10-
test('returns itself', t => {
11-
t.true(t.context.script instanceof Script);
12-
});
13-
14-
test('sets a config object', t => {
15-
const script = new Script(false);
16-
t.true(script instanceof Script);
17-
});
18-
19-
test('renders name', t => {
20-
const { script } = t.context;
21-
t.is(script.renderName(), 'script');
22-
});
23-
24-
test('sets a default name', t => {
25-
const { script } = t.context;
26-
t.is(script._name, 'script');
5+
test('picks original', t => {
6+
t.deepEqual(
7+
pickOriginal(
8+
{
9+
foo: {
10+
boop: { baz: false },
11+
array: [0, 1, 2, 3, 4, 5, 6, 7, 8],
12+
arr: [
13+
{
14+
baz: true,
15+
bar: false,
16+
beep: {
17+
foo: [
18+
1,
19+
{
20+
baz: 'foo'
21+
},
22+
3
23+
]
24+
}
25+
}
26+
]
27+
}
28+
},
29+
{
30+
foo: {
31+
array: [0, 1, 2, 3, 4],
32+
arr: [
33+
{
34+
baz: true,
35+
beep: {
36+
foo: [
37+
1,
38+
{
39+
baz: 'foo'
40+
}
41+
]
42+
}
43+
}
44+
]
45+
}
46+
}
47+
),
48+
{
49+
foo: {
50+
array: [0, 1, 2, 3, 4],
51+
arr: [
52+
{
53+
baz: true,
54+
beep: {
55+
foo: [
56+
1,
57+
{
58+
baz: 'foo'
59+
}
60+
]
61+
}
62+
}
63+
]
64+
}
65+
}
66+
);
2767
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,6 +4078,11 @@ nlcst-to-string@^2.0.0:
40784078
resolved "https://registry.yarnpkg.com/nlcst-to-string/-/nlcst-to-string-2.0.4.tgz#9315dfab80882bbfd86ddf1b706f53622dc400cc"
40794079
integrity sha512-3x3jwTd6UPG7vi5k4GEzvxJ5rDA7hVUIRNHPblKuMVP9Z3xmlsd9cgLcpAMkc5uPOBna82EeshROFhsPkbnTZg==
40804080

4081+
node-dotify@^1.0.1:
4082+
version "1.0.1"
4083+
resolved "https://registry.yarnpkg.com/node-dotify/-/node-dotify-1.0.1.tgz#ab2eab6d4a01ce37c61a04983c763ed07c29fe46"
4084+
integrity sha1-qy6rbUoBzjfGGgSYPHY+0Hwp/kY=
4085+
40814086
node-fetch@^2.2.0:
40824087
version "2.6.0"
40834088
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"

0 commit comments

Comments
 (0)