Skip to content

Commit 70f4784

Browse files
committed
♻️ specify target types in extension object
Signed-off-by: Nahuel Garbezza <n.garbezza@gmail.com>
1 parent 55aa0ff commit 70f4784

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

src/extensions/collections.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { applyExtension } from './extension_applier.js';
44

55
const Collection = {
6+
targets: [Array, String, Set],
67
class: {
78
},
89
instance: {
@@ -49,6 +50,7 @@ const Collection = {
4950
};
5051

5152
const SequenceableCollection = {
53+
targets: [Array, String],
5254
class: {
5355
},
5456
instance: {
@@ -103,6 +105,7 @@ const SequenceableCollection = {
103105
};
104106

105107
const HeterogeneousCollection = {
108+
targets: [Array, Set],
106109
class: {
107110
},
108111
instance: {
@@ -128,6 +131,7 @@ const HeterogeneousCollection = {
128131
};
129132

130133
const ArrayExtensions = {
134+
targets: [Array],
131135
class: {
132136
with(...objects) {
133137
return objects;
@@ -162,6 +166,7 @@ const ArrayExtensions = {
162166
};
163167

164168
const StringExtensions = {
169+
targets: [String],
165170
class: {
166171
},
167172
instance: {
@@ -189,6 +194,7 @@ const StringExtensions = {
189194
};
190195

191196
const SetExtensions = {
197+
targets: [Set],
192198
class: {
193199
with(...objects) {
194200
return objects.asSet();
@@ -240,10 +246,10 @@ const SetExtensions = {
240246
};
241247

242248
export const install = () => {
243-
applyExtension(Collection, Array, String, Set);
244-
applyExtension(SequenceableCollection, Array, String);
245-
applyExtension(HeterogeneousCollection, Array, Set);
246-
applyExtension(ArrayExtensions, Array);
247-
applyExtension(StringExtensions, String);
248-
applyExtension(SetExtensions, Set);
249+
applyExtension(Collection);
250+
applyExtension(SequenceableCollection);
251+
applyExtension(HeterogeneousCollection);
252+
applyExtension(ArrayExtensions);
253+
applyExtension(StringExtensions);
254+
applyExtension(SetExtensions);
249255
};

src/extensions/date.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import { applyExtension } from './extension_applier.js';
44

55
const DateExtensions = {
6+
targets: [Date],
67
class: {
78
today() {
8-
return new Date();
9+
return this.new();
910
},
1011
tomorrow() {
1112
const result = this.today();
@@ -32,5 +33,5 @@ const DateExtensions = {
3233
};
3334

3435
export const install = () => {
35-
applyExtension(DateExtensions, Date);
36+
applyExtension(DateExtensions);
3637
};

src/extensions/extension_applier.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const eachExtensionOf = (extension, block) =>
66
const extend = (proto, extension, methodName) =>
77
Object.defineProperty(proto, methodName, { value: extension[methodName] });
88

9-
export const applyExtension = (extension, ...targetTypes) => {
9+
export const applyExtension = (extension) => {
1010
eachExtensionOf(extension.instance, methodName =>
11-
targetTypes.forEach(type => extend(type.prototype, extension.instance, methodName))
11+
extension.targets.forEach(type => extend(type.prototype, extension.instance, methodName))
1212
);
1313
eachExtensionOf(extension.class, methodName =>
14-
targetTypes.forEach(type => extend(type, extension.class, methodName))
14+
extension.targets.forEach(type => extend(type, extension.class, methodName))
1515
);
1616
};

src/extensions/function.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { applyExtension } from './extension_applier.js';
44

55
const FunctionExtension = {
6+
targets: [Function],
67
class: {
78
},
89
instance: {
@@ -17,5 +18,5 @@ const FunctionExtension = {
1718
};
1819

1920
export const install = () => {
20-
applyExtension(FunctionExtension, Function);
21+
applyExtension(FunctionExtension);
2122
};

src/extensions/number.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { applyExtension } from './extension_applier.js';
44

55
const NumberExtensions = {
6+
targets: [Number],
67
instance: {
78
isDivisibleBy(aNumber) {
89
return this % aNumber === 0;
@@ -32,5 +33,5 @@ const NumberExtensions = {
3233
};
3334

3435
export const install = () => {
35-
applyExtension(NumberExtensions, Number);
36+
applyExtension(NumberExtensions);
3637
};

src/extensions/object.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { applyExtension } from './extension_applier.js';
44

55
const ObjectExtension = {
6+
targets: [Object],
67
class: {
78
},
89
instance: {
@@ -25,5 +26,5 @@ const ObjectExtension = {
2526
};
2627

2728
export const install = () => {
28-
applyExtension(ObjectExtension, Object);
29+
applyExtension(ObjectExtension);
2930
};

0 commit comments

Comments
 (0)