8000 Merge pull request #643 from PolyPik/master · twigjs/twig.js@040051e · GitHub
[go: up one dir, main page]

Skip to content

Commit 040051e

Browse files
authored
Merge pull request #643 from PolyPik/master
Clean up twig.lib.js
2 parents 09381c2 + 8a2fd3a commit 040051e

File tree

6 files changed

+53
-94
lines changed

6 files changed

+53
-94
lines changed

src/twig.core.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,13 @@ module.exports = function (Twig) {
10781078
blocks = this.template.parentTemplate.getBlocks();
10791079
}
10801080

1081-
// Override with any blocks defined within the associated template
1082-
Twig.lib.extend(blocks, this.template.getBlocks());
1083-
1084-
// Override with any blocks specified when initialized
1085-
Twig.lib.extend(blocks, this.overrideBlocks);
1081+
blocks = {
1082+
...blocks,
1083+
// Override with any blocks defined within the associated template
1084+
...this.template.getBlocks(),
1085+
// Override with any blocks specified when initialized
1086+
...this.overrideBlocks
1087+
};
10861088

10871089
return blocks;
10881090
};
@@ -1305,11 +1307,13 @@ module.exports = function (Twig) {
13051307
Twig.Template.prototype.getBlocks = function () {
13061308
let blocks = {};
13071309

1308-
// Get any blocks imported from other templates
1309-
blocks = Twig.lib.extend(blocks, this.blocks.imported);
1310-
1311-
// Override with any blocks defined within the template itself
1312-
Twig.lib.extend(blocks, this.blocks.defined);
1310+
blocks = {
1311+
...blocks,
1312+
// Get any blocks imported from other templates
1313+
...this.blocks.imported,
1314+
// Override with any blocks defined within the template itself
1315+
...this.blocks.defined
1316+
};
13131317

13141318
return blocks;
13151319
};

src/twig.expression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ module.exports = function (Twig) {
11901190

11911191
matchFound = false;
11921192

1193-
if (Twig.lib.isArray(regex)) {
1193+
if (Array.isArray(regex)) {
11941194
regexI = regex.length;
11951195
while (regexI-- > 0) {
11961196
expression = expression.replace(regex[regexI], matchFunction);
@@ -1280,7 +1280,7 @@ module.exports = function (Twig) {
12801280
const state = this;
12811281

12821282
// If the token isn't an array, make it one.
1283-
if (!Twig.lib.isArray(tokens)) {
1283+
if (!Array.isArray(tokens)) {
12841284
tokens = [tokens];
12851285
}
12861286

src/twig.filters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ module.exports = function (Twig) {
161161
const resultValue = obj[key];
162162

163163
result.push(
164-
(Twig.lib.is('Object', resultValue) || Twig.lib.isArray(resultValue)) ?
164+
(Twig.lib.is('Object', resultValue) || Array.isArray(resultValue)) ?
165165
serialize(resultValue, resultKey) :
166166
encodeURIComponent(resultKey) + '=' + encodeURIComponent(resultValue)
167167
);

src/twig.lib.js

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,34 @@ module.exports = function (Twig) {
2020
Twig.lib.date = require('locutus/php/datetime/date');
2121
Twig.lib.boolval = require('locutus/php/var/boolval');
2222

23-
const {toString} = Object.prototype;
24-
2523
Twig.lib.is = function (type, obj) {
2624
if (typeof obj === 'undefined' || obj === null) {
2725
return false;
2826
}
2927

30-
if (type === 'Array' && Array.isArray) {
31-
return Array.isArray(obj);
32-
}
33-
34-
return toString.call(obj).slice(8, -1) === type;
35-
};
36-
37-
Twig.lib.isArray = Array.isArray || function (obj) {
38-
return toString.call(obj).slice(8, -1) === 'Array';
39-
};
40-
41-
// Shallow-copy an object
42-
Twig.lib.copy = function (src) {
43-
const target = {};
44-
let key;
45-
for (key in src) {
46-
if (Object.hasOwnProperty.call(src, key)) {
47-
target[key] = src[key];
48-
}
28+
switch (type) {
29+
case 'Array':
30+
return Array.isArray(obj);
31+
case 'Date':
32+
return obj instanceof Date;
33+
case 'String':
34+
return (typeof obj === 'string' || obj instanceof String);
35+
case 'Number':
36+
return (typeof obj === 'number' || obj instanceof Number);
37+
case 'Function':
38+
return (typeof obj === 'function');
39+
case 'Object':
40+
return obj instanceof Object;
41+
default:
42+
return false;
4943
}
50-
51-
return target;
52-
};
53-
54-
Twig.lib.extend = function (src, add) {
55-
const keys = Object.keys(add || {});
56-
let i;
57-
58-
i = keys.length;
59-
60-
while (i--) {
61-
src[keys[i]] = add[keys[i]];
62-
}
63-
64-
return src;
6544
};
6645

6746
Twig.lib.replaceAll = function (string, search, replace) {
68-
return string.split(search).join(replace);
47+
// Escape possible regular expression syntax
48+
const searchEscaped = search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
49+
50+
return string.replace(new RegExp(searchEscaped, 'g'), replace);
6951
};
7052

7153
// Chunk an array (arr) into arrays of (size) items, returns an array of arrays, or an empty array on invalid input
@@ -74,7 +56,7 @@ module.exports = function (Twig) {
7456
let x = 0;
7557
const len = arr.length;
7658

77-
if (size < 1 || !Twig.lib.is('Array', arr)) {
59+
if (size < 1 || !Array.isArray(arr)) {
7860
return [];
7961
}
8062

src/twig.logic.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ module.exports = function (Twig) {
275275

276276
// Run once for each iteration of the loop
277277
const loop = function (key, value) {
278-
const innerContext = Twig.lib.copy(context);
278+
const innerContext = {...context};
279279

280280
innerContext[token.valueVar] = value;
281281

@@ -315,7 +315,7 @@ module.exports = function (Twig) {
315315

316316
return Twig.expression.parseAsync.call(state, token.expression, context)
317317
.then(result => {
318-
if (Twig.lib.isArray(result)) {
318+
if (Array.isArray(result)) {
319319
len = result.length;
320320
return Twig.async.forEach(result, value => {
321321
const key = index;
@@ -401,7 +401,7 @@ module.exports = function (Twig) {
401401
Otherwise we have a context with infinite recursion.
402402
Fixes #341
403403
*/
404-
value = Twig.lib.copy(value);
404+
value = {...value};
405405
}
406406

407407
context[key] = value;
@@ -660,7 +660,10 @@ module.exports = function (Twig) {
660660
const useState = new Twig.ParseState(useTemplate);
661661
return useState.parseAsync(useTemplate.tokens)
662662
.then(() => {
663-
Twig.lib.extend(state.template.blocks.imported, useState.getBlocks());
663+
state.template.blocks.imported = {
664+
...state.template.blocks.imported,
665+
...useState.getBlocks()
666+
};
664667
});
665668
})
666669
.then(() => {
@@ -709,7 +712,7 @@ module.exports = function (Twig) {
709712
},
710713
parse(token, context, chain) {
711714
// Resolve filename
712-
const innerContext = token.only ? {} : Twig.lib.copy(context);
715+
let innerContext = token.only ? {} : {...context};
713716
const {ignoreMissing} = token;
714717
const state = this;
715718
let promise = null;
@@ -720,7 +723,10 @@ module.exports = function (Twig) {
720723
} else {
721724
promise = Twig.expression.parseAsync.call(state, token.withStack, context)
722725
.then(withContext => {
723-
Twig.lib.extend(innerContext, withContext);
726+
innerContext = {
727+
...innerContext,
728+
...withContext
729+
};
724730
});
725731
}
726732

@@ -1077,12 +1083,12 @@ module.exports = function (Twig) {
10771083
let state = this;
10781084

10791085
if (!token.only) {
1080-
embedContext = Twig.lib.copy(context);
1086+
embedContext = {...context};
10811087
}
10821088

10831089
if (token.withStack !== undefined) {
10841090
promise = Twig.expression.parseAsync.call(state, token.withStack, context).then(withContext => {
1085-
Twig.lib.extend(embedContext, withContext);
1091+
embedContext = {...embedContext, ...withContext};
10861092
});
10871093
}
10881094

@@ -1180,7 +1186,7 @@ module.exports = function (Twig) {
11801186
let promise = Twig.Promise.resolve();
11811187

11821188
if (!token.only) {
1183-
innerContext = Twig.lib.copy(context);
1189+
innerContext = {...context};
11841190
}
11851191

11861192
if (token.withStack !== undefined) {
@@ -1312,7 +1318,7 @@ module.exports = function (Twig) {
13121318

13131319
// Handle multiple regular expressions per type.
13141320
regexArray = tokenRegex;
1315-
if (!Twig.lib.isArray(tokenRegex)) {
1321+
if (!Array.isArray(tokenRegex)) {
13161322
regexArray = [tokenRegex];
13171323
}
13181324

test/test.lib.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0