generated from fastify/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
171 lines (147 loc) · 4.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict'
// based on https://github.com/TehShrike/deepmerge
// MIT License
// Copyright (c) 2012 - 2022 James Halliday, Josh Duff, and other contributors of deepmerge
const JSON_PROTO = Object.getPrototypeOf({})
function deepmergeConstructor (options) {
function isNotPrototypeKey (value) {
return (
value !== 'constructor' &&
value !== 'prototype' &&
value !== '__proto__'
)
}
function cloneArray (value) {
let i = 0
const il = value.length
const result = new Array(il)
for (i = 0; i < il; ++i) {
result[i] = clone(value[i])
}
return result
}
function cloneObject (target) {
const result = {}
if (cloneProtoObject && Object.getPrototypeOf(target) !== JSON_PROTO) {
return cloneProtoObject(target)
}
const targetKeys = getKeys(target)
let i, il, key
for (i = 0, il = targetKeys.length; i < il; ++i) {
isNotPrototypeKey(key = targetKeys[i]) &&
(result[key] = clone(target[key]))
}
return result
}
function concatArrays (target, source) {
const tl = target.length
const sl = source.length
let i = 0
const result = new Array(tl + sl)
for (i = 0; i < tl; ++i) {
result[i] = clone(target[i])
}
for (i = 0; i < sl; ++i) {
result[i + tl] = clone(source[i])
}
return result
}
const propertyIsEnumerable = Object.prototype.propertyIsEnumerable
function getSymbolsAndKeys (value) {
const result = Object.keys(value)
const keys = Object.getOwnPropertySymbols(value)
for (let i = 0, il = keys.length; i < il; ++i) {
propertyIsEnumerable.call(value, keys[i]) && result.push(keys[i])
}
return result
}
const getKeys = options?.symbols
? getSymbolsAndKeys
: Object.keys
const cloneProtoObject = typeof options?.cloneProtoObject === 'function'
? options.cloneProtoObject
: undefined
function isMergeableObject (value) {
return typeof value === 'object' && value !== null && !(value instanceof RegExp) && !(value instanceof Date)
}
function isPrimitive (value) {
return typeof value !== 'object' || value === null
}
const isPrimitiveOrBuiltIn = typeof Buffer !== 'undefined'
? (value) => typeof value !== 'object' || value === null || value instanceof RegExp || value instanceof Date || value instanceof Buffer
: (value) => typeof value !== 'object' || value === null || value instanceof RegExp || value instanceof Date
const mergeArray = options && typeof options.mergeArray === 'function'
? options.mergeArray({ clone, deepmerge: _deepmerge, getKeys, isMergeableObject })
: concatArrays
function clone (entry) {
return isMergeableObject(entry)
? Array.isArray(entry)
? cloneArray(entry)
: cloneObject(entry)
: entry
}
function mergeObject (target, source) {
const result = {}
const targetKeys = getKeys(target)
const sourceKeys = getKeys(source)
let i, il, key
for (i = 0, il = targetKeys.length; i < il; ++i) {
isNotPrototypeKey(key = targetKeys[i]) &&
(sourceKeys.indexOf(key) === -1) &&
(result[key] = clone(target[key]))
}
for (i = 0, il = sourceKeys.length; i < il; ++i) {
if (!isNotPrototypeKey(key = sourceKeys[i])) {
continue
}
if (key in target) {
if (targetKeys.indexOf(key) !== -1) {
if (cloneProtoObject && isMergeableObject(source[key]) && Object.getPrototypeOf(source[key]) !== JSON_PROTO) {
result[key] = cloneProtoObject(source[key])
} else {
result[key] = _deepmerge(target[key], source[key])
}
}
} else {
result[key] = clone(source[key])
}
}
return result
}
function _deepmerge (target, source) {
const sourceIsArray = Array.isArray(source)
const targetIsArray = Array.isArray(target)
if (isPrimitive(source)) {
return source
} else if (isPrimitiveOrBuiltIn(target)) {
return clone(source)
} else if (sourceIsArray && targetIsArray) {
return mergeArray(target, source)
} else if (sourceIsArray !== targetIsArray) {
return clone(source)
} else {
return mergeObject(target, source)
}
}
function _deepmergeAll () {
switch (arguments.length) {
case 0:
return {}
case 1:
return clone(arguments[0])
case 2:
return _deepmerge(arguments[0], arguments[1])
}
let result
for (let i = 0, il = arguments.length; i < il; ++i) {
result = _deepmerge(result, arguments[i])
}
return result
}
return options?.all
? _deepmergeAll
: _deepmerge
}
module.exports = deepmergeConstructor
module.exports.default = deepmergeConstructor
module.exports.deepmerge = deepmergeConstructor