Skip to content

Commit

Permalink
feat: add matchesGlob export (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 3, 2025
1 parent 8f433f2 commit 0888daf
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ types
.eslintcache
*.log*
*.env*
tmp
28 changes: 27 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

--------------------------------------------------------------------------------
---

Copyright Joyent, Inc. and other Node contributors.

Expand All @@ -42,3 +42,29 @@ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

---

Bundled zeptomatch (https://github.com/fabiospampinato/zeptomatch)

The MIT License (MIT)

Copyright (c) 2023-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { dirname, resolve } from "node:path";
import { writeFile, mkdir } from "node:fs/promises";
import { defineBuildConfig } from "unbuild";
import { build, type BuildOptions, transform } from "esbuild";

export default defineBuildConfig({
hooks: {
async "build:before"(ctx) {
ctx.options.alias["zeptomatch"] = await buildZeptomatch();
},
},
});

async function buildZeptomatch() {
let bundle = await build(<BuildOptions>{
format: "iife",
globalName: "__lib__",
bundle: true,
write: false,
stdin: {
resolveDir: process.cwd(),
contents: /* js */ `export { default as zeptomatch } from "zeptomatch";`,
},
}).then((r) => r.outputFiles![0].text);

bundle = (await transform(bundle, { minify: true })).code!;

bundle = /* js */ `
let _lazyMatch = () => { ${bundle}; return __lib__.default || __lib__; };
let _match;
export default (path, pattern) => {
if (!_match) {
_match = _lazyMatch();
_lazyMatch = null;
}
return _match(path, pattern);
};
`;

const outFile = resolve("tmp/node_modules/zeptomatch/zeptomatch.min.mjs");
await mkdir(dirname(outFile), { recursive: true });
await writeFile(outFile, bundle);
return outFile;
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@
"@types/node": "^22.10.2",
"@vitest/coverage-v8": "^2.1.8",
"changelogen": "^0.5.7",
"esbuild": "^0.24.2",
"eslint": "^9.17.0",
"eslint-config-unjs": "^0.4.2",
"jiti": "^2.4.2",
"prettier": "^3.4.2",
"typescript": "^5.7.2",
"unbuild": "^3.2.0",
"vitest": "^2.1.8"
"vitest": "^2.1.8",
"zeptomatch": "^2.0.0"
},
"packageManager": "[email protected]"
}
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import * as path from "./path";

export * from "./path";

export type Pathe = Omit<
typeof NodePath,
"posix" | "win32" | "matchesGlob" // https://github.com/unjs/pathe/issues/182
>;
export type Pathe = Omit<typeof NodePath, "posix" | "win32">;

export const posix = path satisfies Pathe;

Expand Down
14 changes: 14 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Check LICENSE file

import type path from "node:path";

import zeptomatch from "zeptomatch";

import { normalizeWindowsPath } from "./_internal";

const _UNC_REGEX = /^[/\\]{2}/;
Expand Down Expand Up @@ -290,3 +292,15 @@ export const parse: typeof path.parse = function (p) {
name: base.slice(0, base.length - extension.length),
};
};

/**
* The `path.matchesGlob()` method determines if `path` matches the `pattern`.
* @param path The path to glob-match against.
* @param pattern The glob to check the path against.
*/
export const matchesGlob = (
path: string,
pattern: string | string[],
): boolean => {
return zeptomatch(pattern, normalize(path));
};
7 changes: 7 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
sep as separator,
toNamespacedPath,
normalizeString,
matchesGlob,
} from "../src";

import { normalizeWindowsPath } from "../src/_internal";
Expand Down Expand Up @@ -401,6 +402,12 @@ describe("constants", () => {
});
});

runTest("matchesGlob", matchesGlob, [
["/foo/bar", "/foo/**", true],
[String.raw`\foo\bar`, "/foo/**", true],
["/foo/bar", "/{bar,foo}/**", true],
]);

function _s(item) {
return (JSON.stringify(_r(item)) || "undefined").replace(/"/g, "'");
}
Expand Down

0 comments on commit 0888daf

Please sign in to comment.