Skip to content

Commit 39f612a

Browse files
authored
Merge branch 'main' into develop/misc
2 parents 1debb0c + bb49365 commit 39f612a

File tree

6 files changed

+408
-15
lines changed

6 files changed

+408
-15
lines changed

.github/workflows/ci.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
name: Node Unit Tests
12
on:
23
push:
34
branches-ignore:
@@ -8,14 +9,15 @@ jobs:
89
strategy:
910
matrix:
1011
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
11-
node: ["16", "18", "20"]
12+
node: ["16", "18", "20", "22"]
1213
name: Node.js ${{ matrix.node }} on ${{ matrix.os }}
1314
steps:
14-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1516
- name: Setup node
16-
uses: actions/setup-node@v3
17+
uses: actions/setup-node@v4
1718
with:
1819
node-version: ${{ matrix.node }}
20+
# cache: npm
1921
- run: npm install
2022
- run: npm test
2123
env:

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const TemplatePath = require("./src/TemplatePath.js");
22
const isPlainObject = require("./src/IsPlainObject.js");
3+
const Merge = require("./src/Merge.js");
4+
const { DeepCopy } = Merge;
35

46
module.exports = {
57
TemplatePath,
68
isPlainObject,
9+
Merge,
10+
DeepCopy,
711
};

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@11ty/eleventy-utils",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Low level internal utilities to be shared amongst Eleventy projects",
55
"main": "index.js",
66
"files": [
@@ -38,10 +38,7 @@
3838
},
3939
"bugs": "https://github.com/11ty/eleventy-utils/issues",
4040
"homepage": "https://github.com/11ty/eleventy-utils/",
41-
"dependencies": {
42-
"normalize-path": "^3.0.0"
43-
},
4441
"devDependencies": {
45-
"ava": "^4.1.0"
42+
"ava": "^6.1.3"
4643
}
4744
}

src/Merge.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"use strict";
2+
// above is required for Object.freeze to fail correctly.
3+
4+
const isPlainObject = require("./IsPlainObject.js");
5+
6+
const OVERRIDE_PREFIX = "override:";
7+
8+
function cleanKey(key, prefix) {
9+
if (prefix && key.startsWith(prefix)) {
10+
return key.slice(prefix.length);
11+
}
12+
return key;
13+
}
14+
15+
function getMergedItem(target, source, prefixes = {}) {
16+
let { override } = prefixes;
17+
18+
// Shortcut for frozen source (if target does not exist)
19+
if (!target && isPlainObject(source) && Object.isFrozen(source)) {
20+
return source;
21+
}
22+
23+
let sourcePlainObjectShortcut;
24+
if (!target && isPlainObject(source)) {
25+
// deep copy objects to avoid sharing and to effect key renaming
26+
target = {};
27+
sourcePlainObjectShortcut = true;
28+
}
29+
30+
if (Array.isArray(target) && Array.isArray(source)) {
31+
return target.concat(source);
32+
} else if (isPlainObject(target)) {
33+
if (sourcePlainObjectShortcut || isPlainObject(source)) {
34+
for (let key in source) {
35+
let overrideKey = cleanKey(key, override);
36+
37+
// An error happens here if the target is frozen
38+
target[overrideKey] = getMergedItem(target[key], source[key], prefixes);
39+
}
40+
}
41+
return target;
42+
}
43+
// number, string, class instance, etc
44+
return source;
45+
}
46+
47+
// The same as Merge but without override prefixes
48+
function DeepCopy(targetObject, ...sources) {
49+
for (let source of sources) {
50+
if (!source) {
51+
continue;
52+
}
53+
54+
targetObject = getMergedItem(targetObject, source);
55+
}
56+
return targetObject;
57+
}
58+
59+
function Merge(target, ...sources) {
60+
// Remove override prefixes from root target.
61+
if (isPlainObject(target)) {
62+
for (let key in target) {
63+
if (key.indexOf(OVERRIDE_PREFIX) === 0) {
64+
target[key.slice(OVERRIDE_PREFIX.length)] = target[key];
65+
delete target[key];
66+
}
67+
}
68+
}
69+
70+
for (let source of sources) {
71+
if (!source) {
72+
continue;
73+
}
74+
75+
target = getMergedItem(target, source, {
76+
override: OVERRIDE_PREFIX,
77+
});
78+
}
79+
80+
return target;
81+
}
82+
83+
module.exports = Merge;
84+
module.exports.DeepCopy = DeepCopy;

src/TemplatePath.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require("path");
2-
const normalize = require("normalize-path");
32
const fs = require("fs");
43

54
function TemplatePath() {}
@@ -59,7 +58,7 @@ TemplatePath.getLastPathSegment = function (path) {
5958
// Trim a trailing slash if there is one
6059
path = path.replace(/\/$/, "");
6160

62-
return path.substring(path.lastIndexOf("/") + 1);
61+
return path.slice(path.lastIndexOf("/") + 1);
6362
};
6463

6564
/**
@@ -93,22 +92,25 @@ TemplatePath.getAllDirs = function (path) {
9392
* @returns {String} the normalized path.
9493
*/
9594
TemplatePath.normalize = function (thePath) {
96-
return normalize(path.normalize(thePath));
95+
let filePath = path.normalize(thePath).split(path.sep).join("/");
96+
if(filePath !== "/" && filePath.endsWith("/")) {
97+
return filePath.slice(0, -1);
98+
}
99+
return filePath;
97100
};
98101

99102
/**
100103
* Joins all given path segments together.
101104
*
102-
* It uses Node.js’ [`path.join`][1] method and the [normalize-path][2] package.
105+
* It uses Node.js’ [`path.join`][1] method.
103106
*
104107
* [1]: https://nodejs.org/api/path.html#path_path_join_paths
105-
* [2]: https://www.npmjs.com/package/normalize-path
106108
*
107109
* @param {...String} paths - An arbitrary amount of path segments.
108110
* @returns {String} the normalized and joined path.
109111
*/
110112
TemplatePath.join = function (...paths) {
111-
return normalize(path.join(...paths));
113+
return TemplatePath.normalize(path.join(...paths));
112114
};
113115

114116
/**
@@ -236,7 +238,7 @@ TemplatePath.stripLeadingSubPath = function (path, subPath) {
236238
subPath = TemplatePath.normalize(subPath);
237239

238240
if (subPath !== "." && path.startsWith(subPath)) {
239-
return path.substring(subPath.length + 1);
241+
return path.slice(subPath.length + 1);
240242
}
241243

242244
return path;

0 commit comments

Comments
 (0)