Associate value to urls using pattern matching.
npm install @jsenv/url-metaAssociate value to urls using pattern matching.
``js
import { URL_META } from "@jsenv/url-meta";
// conditionally associates url and values
const associations = {
color: {
"file:///*": "black",
"file:///*.js": "red",
},
};
const getUrlColor = (url) => {
const { color } = URL_META.applyAssociations({ url, associations });
return color;
};
console.log(file.json color is ${getUrlColor("file:///file.json")});file.js color is ${getUrlColor("file:///file.js")}
console.log();`
_Code above logs_
`console`
file.json color is black
file.js color is red
| pattern | Description |
| ------------------ | ------------------------------------ |
| **/ | Everything |/*/
| | Inside a directory |*/./
| | Inside directory starting with a dot |**/node_modules/
| | Inside any node_modules directory |node_modules/
| | Inside root node_modules directory |*/.map
| | Ending with .map |*/.test.*
| | Contains .test. |*
| | Inside the root directory only |/
| | Inside a directory of depth 1 |
Read more at ./pattern_matching.md
_associations_ below translates into: "files are visible except thoose in .git/ directory"
`js`
const associations = {
visible: {
"*//": true,
"**/.git/": false,
},
};
_associations_ allows to group patterns per property which are easy to read and compose.
All keys in _associations_ must be absolute urls, this can be done with resolveAssociations.
_resolveAssociations_ is a function resolving _associations_ keys that may contain relative urls against an _url_.
`js
import { URL_META } from "@jsenv/url-meta";
const associations = URL_META.resolveAssociations(
{
visible: {
"*//": true,
"**/.git/": false,
},
},
"file:///Users/directory/",
);
console.log(JSON.stringify(associations, null, " "));
`
`json``
{
"visible": {
"file:///Users/directory/*//": true,
"file:///Users/directory/**/.git/": false
}
}