Skip to content

Commit d8e5747

Browse files
committed
feat(all): initial commit
0 parents  commit d8e5747

File tree

10 files changed

+257
-0
lines changed

10 files changed

+257
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
.idea
3+
.vscode
4+
npm-debug.log*
5+
/typings

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/typings
2+
/.vscode

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Bazyli Brzóska
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# easy-assign
2+
3+
Easily deep assign objects and merge arrays inside of them.

package.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@easy-webpack/assign",
3+
"version": "1.0.0",
4+
"description": "Easily deep assign objects and merge arrays inside of them",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"test": "TS_NODE_FAST=true TS_NODE_NO_PROJECT=true ava",
8+
"prepublish": "tsc -p ."
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/easy-webpack/assign.git"
13+
},
14+
"keywords": [
15+
"object.assign",
16+
"assign",
17+
"append",
18+
"array",
19+
"configurator",
20+
"configuration",
21+
"config",
22+
"simple"
23+
],
24+
"author": "Bazyli Brzóska <bazyli.brzoska@gmail.com> (https://invent.life)",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/easy-webpack/assign/issues"
28+
},
29+
"homepage": "https://github.com/easy-webpack/assign#readme",
30+
"devDependencies": {
31+
"ava": "^0.15.2",
32+
"semantic-release": "^4.3.5",
33+
"ts-node": "^0.9.1",
34+
"tslint": "^3.11.0",
35+
"tslint-config-standard": "^1.2.2",
36+
"typescript": ">=1.9.0-dev || ^2.0.0"
37+
},
38+
"dependencies": {
39+
},
40+
"ava": {
41+
"files": [
42+
"test/**/*.{ts,js}"
43+
],
44+
"tap": false,
45+
"require": [
46+
"ts-node/register"
47+
]
48+
}
49+
}

src/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export function assign(current, addition, property: string = 'config', style: 'append' | 'prepend' | 'replace' = 'replace') {
2+
if (addition === undefined) {
3+
return current
4+
}
5+
if (Array.isArray(current) && Array.isArray(addition)) {
6+
switch (style) {
7+
case 'append': return (current as Array<any>).concat(...addition)
8+
case 'prepend': return (addition as Array<any>).concat(...current)
9+
default: return addition.slice()
10+
}
11+
}
12+
else if (Array.isArray(addition)) {
13+
return addition.slice()
14+
}
15+
else if (typeof addition === 'object') {
16+
if (typeof current !== 'object' || addition['_literalReplace'])
17+
current = {}
18+
else
19+
current = Object.assign({}, current)
20+
21+
for (let subProperty of Object.getOwnPropertyNames(addition)) {
22+
current[subProperty] = assign(current[subProperty], addition[subProperty], `${property}.${subProperty}`, style)
23+
}
24+
return current
25+
}
26+
return addition
27+
}
28+
29+
export function literalReplace<T>(object: T): T {
30+
object['_literalReplace'] = true
31+
return object
32+
}
33+
34+
export default assign;

test/assign.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import test from 'ava'
2+
import {assign} from '../src'
3+
4+
test(`replace object | 1st degree`, t => {
5+
const replaced = assign({
6+
a: 'a'
7+
}, {
8+
a: 'b'
9+
}, undefined, 'replace')
10+
t.true(replaced.a == 'b')
11+
})
12+
13+
test(`replace and extend object | 3rd degree`, t => {
14+
const replaced = assign({
15+
a: { a: {
16+
a: 'a',
17+
b: 'b'
18+
} },
19+
c: 'c'
20+
}, {
21+
a: { a: { a: 'b' } },
22+
b: 'b'
23+
}, undefined, 'replace')
24+
t.true(replaced.a.a.a == 'b', 'assignes new value')
25+
t.true(replaced.a.a.b == 'b', 'keeps previously assigned value')
26+
t.true(replaced.b == 'b')
27+
t.true(replaced.c == 'c')
28+
})
29+
30+
test(`don't replace object | 1st degree`, t => {
31+
const replaced = assign({
32+
a: 'a'
33+
}, {
34+
b: 'b'
35+
}, undefined, 'replace')
36+
37+
t.true(replaced.a == 'a')
38+
t.true(replaced.b == 'b')
39+
})
40+
41+
test(`append object | 2nd degree`, t => {
42+
const replaced = assign({
43+
a: { aIn: 'a' }
44+
}, {
45+
a: { bIn: 'b' }
46+
}, undefined, 'append')
47+
48+
t.true(replaced.a.aIn == 'a')
49+
t.true(replaced.a.bIn == 'b')
50+
})
51+
52+
test(`append object | 3rd degree`, t => {
53+
const replaced = assign({
54+
a: { aIn: { aInIn: 'a' } }
55+
}, {
56+
a: { aIn: { bInIn: 'b' } }
57+
}, undefined, 'append')
58+
59+
t.true(replaced.a.aIn.aInIn == 'a')
60+
t.true(replaced.a.aIn.bInIn == 'b')
61+
})
62+
63+
test(`append array | 3rd degree`, t => {
64+
const replaced = assign({
65+
a: { aIn: { aInIn: ['a'] } }
66+
}, {
67+
a: { aIn: { aInIn: ['b'] } }
68+
}, undefined, 'append')
69+
70+
t.true(replaced.a.aIn.aInIn[0] == 'a')
71+
t.true(replaced.a.aIn.aInIn[1] == 'b')
72+
})
73+
74+
test(`prepend array | 3rd degree`, t => {
75+
const replaced = assign({
76+
a: { aIn: { aInIn: ['a'] } }
77+
}, {
78+
a: { aIn: { aInIn: ['b'] } }
79+
}, undefined, 'prepend')
80+
81+
t.true(replaced.a.aIn.aInIn[0] == 'b')
82+
t.true(replaced.a.aIn.aInIn[1] == 'a')
83+
})
84+
85+
test(`string replace | 1st degree`, t => {
86+
const replaced = assign({
87+
a: 'before'
88+
}, {
89+
a: 'after'
90+
}, undefined, 'replace')
91+
92+
t.true(replaced.a == 'after')
93+
})
94+
95+
test(`prepend combinations | multiple degrees`, t => {
96+
const replaced = assign({
97+
obj: { aIn: { aInIn: ['a'], bInIn: 'oldField' } },
98+
arr: ['a']
99+
}, {
100+
obj: { aIn: { aInIn: ['b'], bInIn: 'newField' } },
101+
arr: ['b']
102+
}, undefined, 'prepend')
103+
104+
t.true(replaced.obj.aIn.aInIn.length == 2)
105+
t.true(replaced.obj.aIn.aInIn[0] == 'b')
106+
t.true(replaced.obj.aIn.aInIn[1] == 'a')
107+
t.true(replaced.obj.aIn.bInIn == 'newField', 'change non-object/non-array objects with prepend/append')
108+
t.true(replaced.arr.length == 2)
109+
t.true(replaced.arr[0] == 'b')
110+
t.true(replaced.arr[1] == 'a')
111+
})

tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2015",
4+
"module": "commonjs",
5+
"experimentalDecorators": true,
6+
"moduleResolution": "node",
7+
"allowJs": false,
8+
"declaration": true,
9+
"outDir": "dist",
10+
"rootDir": "src",
11+
"sourceRoot": "src",
12+
"sourceMap": true
13+
},
14+
"exclude": [
15+
"node_modules",
16+
"dist",
17+
"test",
18+
"example"
19+
]
20+
}

tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "tslint-config-standard"
3+
}

typings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"dependencies": {
3+
"debug": "registry:npm/debug#2.0.0+20160511151334",
4+
"lodash": "registry:npm/lodash#4.0.0+20160416211519"
5+
},
6+
"globalDependencies": {
7+
"node": "registry:env/node#6.0.0+20160610031852"
8+
}
9+
}

0 commit comments

Comments
 (0)