@jsenv/url-meta
Advanced tools
Comparing version 6.1.0 to 6.2.0
@@ -26,2 +26,44 @@ const assertUrlLike = (value, name = "url") => { | ||
const isPlainObject = value => { | ||
if (value === null) { | ||
return false; | ||
} | ||
if (typeof value === "object") { | ||
if (Array.isArray(value)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; | ||
const structuredMetaMapToMetaMap = structuredMetaMap => { | ||
if (!isPlainObject(structuredMetaMap)) { | ||
throw new TypeError(`structuredMetaMap must be a plain object, got ${structuredMetaMap}`); | ||
} | ||
const metaMap = {}; | ||
Object.keys(structuredMetaMap).forEach(metaProperty => { | ||
const metaValueMap = structuredMetaMap[metaProperty]; | ||
if (!isPlainObject(metaValueMap)) { | ||
throw new TypeError(`metaValueMap must be plain object, got ${metaValueMap} for ${metaProperty}`); | ||
} | ||
Object.keys(metaValueMap).forEach(pattern => { | ||
const metaValue = metaValueMap[pattern]; | ||
const meta = { | ||
[metaProperty]: metaValue | ||
}; | ||
metaMap[pattern] = pattern in metaMap ? { ...metaMap[pattern], | ||
...meta | ||
} : meta; | ||
}); | ||
}); | ||
return metaMap; | ||
}; | ||
/* | ||
@@ -323,2 +365,27 @@ * | ||
const urlToMeta = ({ | ||
url, | ||
structuredMetaMap | ||
}) => { | ||
assertUrlLike(url); | ||
const metaMap = structuredMetaMapToMetaMap(structuredMetaMap); | ||
return Object.keys(metaMap).reduce((previousMeta, pattern) => { | ||
const { | ||
matched | ||
} = applyPatternMatching({ | ||
pattern, | ||
url | ||
}); | ||
if (matched) { | ||
const meta = metaMap[pattern]; | ||
return { ...previousMeta, | ||
...meta | ||
}; | ||
} | ||
return previousMeta; | ||
}, {}); | ||
}; | ||
const normalizeStructuredMetaMap = (structuredMetaMap, baseUrl) => { | ||
@@ -349,42 +416,34 @@ assertUrlLike(baseUrl, "url"); | ||
const isPlainObject = value => { | ||
if (value === null) { | ||
return false; | ||
} | ||
const applyAliases = ({ | ||
url, | ||
aliases | ||
}) => { | ||
let aliasFullMatchResult; | ||
const aliasMatchingKey = Object.keys(aliases).find(key => { | ||
const aliasMatchResult = applyPatternMatching({ | ||
pattern: key, | ||
url | ||
}); | ||
if (typeof value === "object") { | ||
if (Array.isArray(value)) { | ||
return false; | ||
if (aliasMatchResult.matched) { | ||
aliasFullMatchResult = aliasMatchResult; | ||
return true; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}); | ||
return false; | ||
}; | ||
const structuredMetaMapToMetaMap = structuredMetaMap => { | ||
if (!isPlainObject(structuredMetaMap)) { | ||
throw new TypeError(`structuredMetaMap must be a plain object, got ${structuredMetaMap}`); | ||
if (!aliasMatchingKey) { | ||
return url; | ||
} | ||
const metaMap = {}; | ||
Object.keys(structuredMetaMap).forEach(metaProperty => { | ||
const metaValueMap = structuredMetaMap[metaProperty]; | ||
if (!isPlainObject(metaValueMap)) { | ||
throw new TypeError(`metaValueMap must be plain object, got ${metaValueMap} for ${metaProperty}`); | ||
} | ||
Object.keys(metaValueMap).forEach(pattern => { | ||
const metaValue = metaValueMap[pattern]; | ||
const meta = { | ||
[metaProperty]: metaValue | ||
}; | ||
metaMap[pattern] = pattern in metaMap ? { ...metaMap[pattern], | ||
...meta | ||
} : meta; | ||
}); | ||
}); | ||
return metaMap; | ||
const { | ||
matchGroups | ||
} = aliasFullMatchResult; | ||
const alias = aliases[aliasMatchingKey]; | ||
const parts = alias.split("*"); | ||
const newUrl = parts.reduce((previous, value, index) => { | ||
return `${previous}${value}${index === parts.length - 1 ? "" : matchGroups[index]}`; | ||
}, ""); | ||
return newUrl; | ||
}; | ||
@@ -438,29 +497,4 @@ | ||
const urlToMeta = ({ | ||
url, | ||
structuredMetaMap | ||
}) => { | ||
assertUrlLike(url); | ||
const metaMap = structuredMetaMapToMetaMap(structuredMetaMap); | ||
return Object.keys(metaMap).reduce((previousMeta, pattern) => { | ||
const { | ||
matched | ||
} = applyPatternMatching({ | ||
pattern, | ||
url | ||
}); | ||
export { applyAliases, applyPatternMatching, normalizeStructuredMetaMap, urlCanContainsMetaMatching, urlToMeta }; | ||
if (matched) { | ||
const meta = metaMap[pattern]; | ||
return { ...previousMeta, | ||
...meta | ||
}; | ||
} | ||
return previousMeta; | ||
}, {}); | ||
}; | ||
export { applyPatternMatching, normalizeStructuredMetaMap, urlCanContainsMetaMatching, urlToMeta }; | ||
//# sourceMappingURL=jsenv_url_meta.js.map |
{ | ||
"name": "@jsenv/url-meta", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "Associate data to urls using patterns", | ||
@@ -16,4 +16,3 @@ "license": "MIT", | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
"access": "public" | ||
}, | ||
@@ -26,3 +25,3 @@ "engines": { | ||
".": { | ||
"import": "./index.js", | ||
"import": "./main.js", | ||
"require": "./dist/commonjs/jsenv_url_meta.cjs" | ||
@@ -32,7 +31,7 @@ }, | ||
}, | ||
"main": "./index.js", | ||
"main": "./main.js", | ||
"files": [ | ||
"/dist/", | ||
"/src/", | ||
"/index.js" | ||
"/main.js" | ||
], | ||
@@ -69,2 +68,2 @@ "scripts": { | ||
} | ||
} | ||
} |
153
readme.md
@@ -15,7 +15,5 @@ # url-meta [![npm package](https://img.shields.io/npm/v/@jsenv/url-meta.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/url-meta) [![github ci](https://github.com/jsenv/url-meta/workflows/main/badge.svg)](https://github.com/jsenv/url-meta/actions?workflow=main) [![codecov coverage](https://codecov.io/gh/jsenv/url-meta/branch/master/graph/badge.svg)](https://codecov.io/gh/jsenv/url-meta) | ||
} | ||
const urlToColor = (url) => { | ||
return urlToMeta({ url, structuredMetaMap }).color | ||
} | ||
console.log(`file.json color is ${urlToColor("file:///file.json")}`) | ||
@@ -46,2 +44,4 @@ console.log(`file.js color is ${urlToColor("file:///file.js")}`) | ||
Read more at [./docs/pattern_matching.md](./docs/pattern_matching.md) | ||
# metaMap and structuredMetaMap | ||
@@ -64,76 +64,5 @@ | ||
_structuredMetaMap_ allows to group patterns per property which are easier to read and compose. For this reason it's the object structure used by _@jsenv/url-meta_ API. | ||
_structuredMetaMap_ allows to group patterns per property which are easier to read and compose. | ||
All keys in _structuredMetaMap_ must be resolved against an url, this can be done with [normalizeStructuredMetaMap](#normalizeStructuredMetaMap). | ||
# applyPatternMatching | ||
_applyPatternMatching_ is a function returning a _matchResult_ indicating if and how a _pattern_ matches an _url_. | ||
```js | ||
import { applyPatternMatching } from "@jsenv/url-meta" | ||
const matchResult = applyPatternMatching({ | ||
pattern: "file:///**/*", | ||
url: "file://Users/directory/file.js", | ||
}) | ||
matchResult.matched // true | ||
``` | ||
## pattern | ||
_pattern_ parameter is a string looking like an url but where `*` and `**` can be used so that one specifier can match several url. | ||
This parameter is **required**. | ||
## url | ||
_url_ parameter is a string representing a url. | ||
This parameter is **required**. | ||
## matchResult | ||
_matchResult_ represents if and how a _pattern_ matches an _url_. | ||
### Matching example | ||
```js | ||
import { applyPatternMatching } from "@jsenv/url-meta" | ||
const fullMatch = applyPatternMatching({ | ||
pattern: "file:///**/*", | ||
url: "file:///Users/directory/file.js", | ||
}) | ||
console.log(JSON.stringify(fullMatch, null, " ")) | ||
``` | ||
```json | ||
{ | ||
"matched": true, | ||
"patternIndex": 12, | ||
"urlIndex": 31, | ||
"matchGroups": ["file.js"] | ||
} | ||
``` | ||
### Partial matching example | ||
```js | ||
import { applyPatternMatching } from "@jsenv/url-meta" | ||
const partialMatch = applyPatternMatching({ | ||
pattern: "file:///*.js", | ||
url: "file:///file.jsx", | ||
}) | ||
console.log(JSON.stringify(partialMatch, null, " ")) | ||
``` | ||
```json | ||
{ | ||
"matched": false, | ||
"patternIndex": 12, | ||
"urlIndex": 15, | ||
"matchGroups": ["file"] | ||
} | ||
``` | ||
# normalizeStructuredMetaMap | ||
@@ -167,76 +96,2 @@ | ||
# urlCanContainsMetaMatching | ||
_urlCanContainsMetaMatching_ is a function designed to ignore directory content that would never have specific metas. | ||
```js | ||
import { urlCanContainsMetaMatching } from "@jsenv/url-meta" | ||
const structuredMetaMap = { | ||
color: { | ||
"file:///**/*": "blue", | ||
"file:///**/node_modules/": "green", | ||
}, | ||
} | ||
const firstUrlCanHaveFilesWithColorBlue = urlCanContainsMetaMatching({ | ||
url: "file:///src/", | ||
specifierMetaMap, | ||
predicate: ({ color }) => color === "blue", | ||
}) | ||
firstUrlCanHaveFilesWithColorBlue // true | ||
const secondUrlCanHaveFileWithColorBlue = urlCanContainsMetaMatching({ | ||
url: "file:///node_modules/src/", | ||
specifierMetaMap, | ||
predicate: ({ color }) => color === "blue", | ||
}) | ||
secondUrlCanHaveFileWithColorBlue // false | ||
``` | ||
# urlToMeta | ||
_urlToMeta_ is a function returning an object being the composition of all meta where _pattern_ matched the _url_. | ||
```js | ||
import { urlToMeta } from "@jsenv/url-meta" | ||
const structuredMetaMap = { | ||
insideSrc: { | ||
"file:///src/": true, | ||
}, | ||
extensionIsJs: { | ||
"file:///**/*.js": true, | ||
}, | ||
} | ||
const urlA = "file:///src/file.js" | ||
const urlB = "file:///src/file.json" | ||
console.log( | ||
`${urlA}: ${JSON.stringify( | ||
urlToMeta({ url: urlA, structuredMetaMap }), | ||
null, | ||
" ", | ||
)}`, | ||
) | ||
console.log( | ||
`${urlB}: ${JSON.stringify( | ||
urlToMeta({ url: urlB, structuredMetaMap }), | ||
null, | ||
" ", | ||
)}`, | ||
) | ||
``` | ||
_Console output_ | ||
```console | ||
file:///src/file.js: { | ||
"insideSrc": true, | ||
"extensionIsJs": true | ||
} | ||
file:///src/file.json: { | ||
"insideSrc": true | ||
} | ||
``` | ||
# Installation | ||
@@ -243,0 +98,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101250
17
1266
100