Socket
Socket
Sign inDemoInstall

@jsenv/url-meta

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsenv/url-meta - npm Package Compare versions

Comparing version 4.0.0 to 4.1.0

153

dist/commonjs/main.js

@@ -33,6 +33,16 @@ 'use strict';

specifier,
url
url,
...rest
} = {}) => {
assertUrlLike(specifier, "specifier");
assertUrlLike(url, "url");
if (Object.keys(rest).length) {
throw new Error(`received more parameters than expected.
--- name of unexpected parameters ---
${Object.keys(rest)}
--- name of expected parameters ---
specifier, url`);
}
return applyPatternMatching(specifier, url);

@@ -48,4 +58,5 @@ };

while (true) {
// '' === '' -> pass
// pattern consumed and string consumed
if (remainingPattern === "" && remainingString === "") {
// pass because string fully matched pattern
return pass({

@@ -55,6 +66,7 @@ patternIndex,

});
} // '' === value -> fail
} // pattern consumed, string not consumed
if (remainingPattern === "" && remainingString !== "") {
// fails because string longer than expected
return fail({

@@ -64,14 +76,14 @@ patternIndex,

});
} // pattern === '' -> pass only if pattern is only **
} // from this point pattern is not consumed
// string consumed, pattern not consumed
if (remainingPattern !== "" && remainingString === "") {
// pass because pattern is optionnal
if (remainingString === "") {
// pass because trailing "**" is optional
if (remainingPattern === "**") {
return pass({
patternIndex,
patternIndex: patternIndex + 2,
index
});
} // fail because **/ would expect something like /a
// and **a would expect something like foo/bar/a
} // fail because string shorted than expected

@@ -83,11 +95,33 @@

});
}
} // from this point pattern and string are not consumed
// fast path trailing slash
if (remainingPattern.slice(0, "**".length) === "**") {
patternIndex += `**`.length;
remainingPattern = remainingPattern.slice(`**`.length);
if (remainingPattern === "/") {
// pass because trailing slash matches remaining
return pass({
patternIndex: patternIndex + 1,
index: string.length
});
} // fast path trailing '**'
if (remainingPattern === "**") {
// pass because trailing ** matches remaining
return pass({
patternIndex: patternIndex + 2,
index: string.length
});
} // pattern leading **
if (remainingPattern.slice(0, 2) === "**") {
// consumes "**"
remainingPattern = remainingPattern.slice(2);
patternIndex += 2;
if (remainingPattern[0] === "/") {
patternIndex += "/".length;
remainingPattern = remainingPattern.slice("/".length);
// consumes "/"
remainingPattern = remainingPattern.slice(1);
patternIndex += 1;
} // pattern ending with ** always match remaining string

@@ -122,4 +156,5 @@

if (remainingPattern[0] === "*") {
patternIndex += "*".length;
remainingPattern = remainingPattern.slice("*".length); // la c'est plus délicat, il faut que remainingString
// consumes "*"
remainingPattern = remainingPattern.slice(1);
patternIndex += 1; // la c'est plus délicat, il faut que remainingString
// ne soit composé que de truc !== '/'

@@ -176,16 +211,9 @@

});
} // trailing slash on pattern, -> match remaining
} // consumes next char
if (remainingPattern === "/" && remainingString.length > 1) {
return pass({
patternIndex: patternIndex + 1,
index: string.length
});
}
remainingPattern = remainingPattern.slice(1);
remainingString = remainingString.slice(1);
patternIndex += 1;
index += 1;
remainingPattern = remainingPattern.slice(1);
remainingString = remainingString.slice(1);
continue;

@@ -223,4 +251,4 @@ }

remainingString = remainingString.slice(matchAttempt.index + 1);
index += matchAttempt.index + 1;
remainingString = remainingString.slice(matchAttempt.index + 1);

@@ -278,3 +306,3 @@ if (remainingString === "") {

const metaMapToSpecifierMetaMap = metaMap => {
const metaMapToSpecifierMetaMap = (metaMap, ...rest) => {
if (!isPlainObject(metaMap)) {

@@ -284,2 +312,10 @@ throw new TypeError(`metaMap must be a plain object, got ${metaMap}`);

if (rest.length) {
throw new Error(`received more arguments than expected.
--- number of arguments received ---
${1 + rest.length}
--- number of arguments expected ---
1`);
}
const specifierMetaMap = {};

@@ -313,4 +349,14 @@ Object.keys(metaMap).forEach(metaKey => {

const normalizeSpecifierMetaMap = (specifierMetaMap, url) => {
const normalizeSpecifierMetaMap = (specifierMetaMap, url, ...rest) => {
assertSpecifierMetaMap(specifierMetaMap);
assertUrlLike(url, "url");
if (rest.length) {
throw new Error(`received more arguments than expected.
--- number of arguments received ---
${2 + rest.length}
--- number of arguments expected ---
2`);
}
const specifierMetaMapNormalized = {};

@@ -327,5 +373,11 @@ Object.keys(specifierMetaMap).forEach(specifier => {

specifierMetaMap,
predicate
predicate,
...rest
}) => {
assertUrlLike(url, "url");
assertUrlLike(url, "url"); // the function was meants to be used on url ending with '/'
if (!url.endsWith("/")) {
throw new Error(`url should end with /, got ${url}`);
}
assertSpecifierMetaMap(specifierMetaMap);

@@ -335,8 +387,12 @@

throw new TypeError(`predicate must be a function, got ${predicate}`);
} // we add a trailing slash because we are intested into what will be inside
// this url, not the url itself
// it allows to match pattern for what is inside
}
if (Object.keys(rest).length) {
throw new Error(`received more parameters than expected.
--- name of unexpected parameters ---
${Object.keys(rest)}
--- name of expected parameters ---
url, specifierMetaMap, predicate`);
} // for full match we must create an object to allow pattern to override previous ones
const urlWithTrailingSlash = `${url}/`; // for full match we must create an object to allow pattern to override previous ones

@@ -355,3 +411,3 @@ let fullMatchMeta = {};

specifier,
url: urlWithTrailingSlash
url
});

@@ -378,6 +434,16 @@

url,
specifierMetaMap
specifierMetaMap,
...rest
} = {}) => {
assertUrlLike(url);
assertSpecifierMetaMap(specifierMetaMap);
if (Object.keys(rest).length) {
throw new Error(`received more parameters than expected.
--- name of unexpected parameters ---
${Object.keys(rest)}
--- name of expected parameters ---
url, specifierMetaMap`);
}
return Object.keys(specifierMetaMap).reduce((previousMeta, specifier) => {

@@ -390,5 +456,10 @@ const {

});
return matched ? { ...previousMeta,
...specifierMetaMap[specifier]
} : previousMeta;
if (matched) {
return { ...previousMeta,
...specifierMetaMap[specifier]
};
}
return previousMeta;
}, {});

@@ -395,0 +466,0 @@ };

{
"name": "@jsenv/url-meta",
"description": "Associate data to urls using patterns",
"version": "4.0.0",
"version": "4.1.0",
"license": "MIT",

@@ -6,0 +6,0 @@ "repository": {

@@ -27,3 +27,3 @@ # Url meta

`@jsenv/url-meta` can be used to you associate any information to one or more url at once using pattern matching.
`@jsenv/url-meta` can be used to associate value to urls. You can associate a value to many urls using pattern matching.

@@ -35,8 +35,8 @@ ## Code example

// note how specifierMetaMap object below associates object to urls
const specifierMetaMap = {
"http://your-domain.com/*": {
fromYourDomain: true,
"http://example.com/*": {
color: "black",
},
"http://your-domain.com/*.js": {
"http://example.com/*.js": {
color: "red",

@@ -46,10 +46,9 @@ },

const urlA = "http://your-domain.com/file.json"
const urlB = "http://your-domain.com/file.js"
const urlA = "http://example.com/file.json"
const urlAMeta = urlToMeta({ specifierMetaMap, url: urlA }).color
const urlB = "http://example.com/file.js"
const urlBMeta = urlToMeta({ specifierMetaMap, url: urlB }).color
const urlAMeta = urlToMeta({ specifierMetaMap, url: urlA })
const urlBMeta = urlToMeta({ specifierMetaMap, url: urlB })
console.log(`${urlA}: ${JSON.stringify(urlAMeta, null, " ")}`)
console.log(`${urlB}: ${JSON.stringify(urlBMeta, null, " ")}`)
console.log(`${urlA} color is ${urlAMeta.color}`)
console.log(`${urlB} color is ${urlBMeta.color}`)
```

@@ -60,10 +59,4 @@

```console
http://your-domain.com/file.json: {
"fromYourDomain": true,
"color": "black",
}
http://your-domain.com/file.js: {
"fromYourDomain": true,
"color": "red",
}
http://example.com/file.json color is black
http://example.com/file.js color is red
```

@@ -75,10 +68,11 @@

| specifier | url | matches |
| ------------------ | ---------------------------------- | ------- |
| `/folder` | `http://domain.com/folder/file.js` | false |
| `/folder/*.js` | `http://domain.com/folder/file.js` | true |
| `/folder/**/*.js` | `http://domain.com/folder/file.js` | true |
| `/**/*.js` | `http://domain.com/folder/file.js` | true |
| `/folder/file.js` | `http://domain.com/folder/file.js` | true |
| `/folder/file.jsx` | `http://domain.com/folder/file.js` | false |
| specifier | url | matches |
| -------------------------------------- | ------------------------------------- | ------- |
| `http://example.com/whatever` | `http://example.com/whatever/file.js` | false |
| `http://example.com/whatever/` | `http://example.com/whatever/file.js` | true |
| `http://example.com/whatever/*.js` | `http://example.com/whatever/file.js` | true |
| `http://example.com/whatever/**/*.js` | `http://example.com/whatever/file.js` | true |
| `http://example.com/**/*.js` | `http://example.com/whatever/file.js` | true |
| `http://example.com/whatever/file.js` | `http://example.com/whatever/file.js` | true |
| `http://example.com/whatever/file.jsx` | `http://example.com/whatever/file.js` | false |

@@ -102,6 +96,14 @@ ## api

specifier: "file:///**/*",
url: "file://Users/folder/file.js",
url: "file://Users/directory/file.js",
})
console.log(matchResult.matched)
```
Logs
```console
true
```
#### specifier

@@ -114,3 +116,3 @@

```js
"http://domain.com/**/*.js"
"http://example.com/**/*.js"
```

@@ -125,3 +127,3 @@

```js
"http://domain.com/folder/file.js"
"http://example.com/directory/file.js"
```

@@ -133,25 +135,45 @@

It is returned by `applySpecifierPatternMatching`, an example value could be:
It is returned by `applySpecifierPatternMatching`, see below some example.
##### Matching example
```js
applySpecifierPatternMatching({
specifier: "file:///**/*",
url: "file://Users/directory/file.js",
})
```
Returns
```js
{
matched: false,
index: 4,
patternIndex: 1
matched: true,
index: 31,
patternIndex: 12,
}
```
Meaning `specifier` partially matched `url`.
Meaning `specifier` fully matched `url`.
Or
##### Failing example
```js
applySpecifierPatternMatching({
specifier: "file:///*.js",
url: "file:///file.jsx",
})
```
Returns
```js
{
matched: true,
index: 4,
patternIndex: 1
matched: false,
index: 14,
patternIndex: 14,
}
```
Meaning `specifier` full matched `url`.
Meaning `specifier` partially matched `url` until comparing `url[14]` with `specifier[14]`

@@ -169,25 +191,32 @@ ---

const specifierMetaMap = metaMapToSpecifierMetaMap({
visible: {
"file:///**/*": true,
"file://**/.git": false,
metaMapToSpecifierMetaMap({
show: {
"file:///**/*": "yes",
"file://**/.git/": "no",
},
format: {
"file:///**/*.js": "yes",
"file:///**/*.json": "yes",
"file://**/.git/": "no",
},
})
```
#### metaMap
Returns
> `metaMap` is an object where values are conditionnaly applied by specifiers.
This parameter is **required**, an example value could be:
```js
{
visible: {
"file:///**/*": true,
"file://**/.git": false,
}
"file:///**/*": { show: "yes" },
"file://**/.git": { show: "no", format: "no" },
"file:///**/*.js": { show: "yes", format: "yes" },
"file:///**/*.json": { show: "yes", format: "yes" },
}
```
#### metaMap
> `metaMap` is an object where values are conditionnaly applied by specifiers.
This parameter is **required**.
#### specifierMetaMap

@@ -197,11 +226,4 @@

It is returned by `metaMapToSpecifierMetaMap`, an example value could be:
It is returned by `metaMapToSpecifierMetaMap`.
```js
{
"file:///**/*": { visible: true },
"file://**/.git": { visible: false },
}
```
---

@@ -220,6 +242,6 @@

{
"./**/*": { visible: true },
"./**/.git": { visible: false },
"./**/*/": { visible: true },
"./**/.git/": { visible: false },
},
"file:///Users/folder",
"file:///Users/directory/",
)

@@ -230,3 +252,3 @@ ```

> `urlCanContainsMetaMatching` is a function designed to ignore folder content that would never have specific metas.
> `urlCanContainsMetaMatching` is a function designed to ignore directory content that would never have specific metas.

@@ -240,27 +262,25 @@ Implemented in [src/urlCanContainsMetaMatching/urlCanContainsMetaMatching.js](./src/urlCanContainsMetaMatching/urlCanContainsMetaMatching.js), you can use it as shown below.

"file:///**/*": {
source: true,
color: "blue",
},
"file:///**/node_modules": {
source: false,
color: "green",
},
}
const predicate = ({ source }) => source === true
const bluePredicate = ({ color }) => color === "blue"
const urlA = "file:///node_modules/src"
const urlB = "file:///src"
const urlA = "file:///src/"
const urlACan = urlCanContainsMetaMatching({
url: urlA,
specifierMetaMap,
predicate: bluePredicate,
})
const urlB = "file:///node_modules/src/"
const urlBCan = urlCanContainsMetaMatching({
url: urlB,
specifierMetaMap,
predicate: bluePredicate,
})
console.log(
`${urlA} can contains meta matching source: ${urlCanContainsMetaMatching({
url: urlA,
specifierMetaMap,
predicate,
})}`,
)
console.log(
`${urlB} can contains meta matching source: ${urlCanContainsMetaMatching({
url: urlB,
specifierMetaMap,
predicate,
})}`,
)
console.log(`${urlA} can contains meta matching blue predicate: ${urlACan}`)
console.log(`${urlB} can contains meta matching blue predicate: ${urlBCan}`)
```

@@ -271,4 +291,4 @@

```console
file:///node_modules/src can contains meta matching source: false
file:///src can contains meta matching source: true
file:///src/ can contains meta matching blue predicate: true
file:///node_modules/src/ can contains meta matching blue predicate: false
```

@@ -286,4 +306,4 @@

const specifierMetaMap = {
"file:///src": {
insideSrcDirectory: true,
"file:///src/": {
insideSrc: true,
},

@@ -306,7 +326,7 @@ "file:///**/*.js": {

file:///src/file.js: {
"insideSrcDirectory": true,
"insideSrc": true,
"extensionIsJs": true,
}
file:///src/file.json: {
"insideSrcDirectory": true
"insideSrc": true
}

@@ -317,3 +337,3 @@ ```

If you never installed a jsenv package, read [Installing a jsenv package](https://github.com/jsenv/jsenv-core/blob/master/docs/installing-jsenv-package.md#installing-a-jsenv-package) before going further.
If you have never installed a jsenv package, read [Installing a jsenv package](https://github.com/jsenv/jsenv-core/blob/master/docs/installing-jsenv-package.md#installing-a-jsenv-package) before going further.

@@ -323,7 +343,7 @@ This documentation is up-to-date with a specific version so prefer any of the following commands

```console
npm install --save-dev @jsenv/url-meta@4.0.0
npm install --save-dev @jsenv/url-meta@4.1.0
```
```console
yarn add --dev @jsenv/url-meta@4.0.0
yarn add --dev @jsenv/url-meta@4.1.0
```

@@ -6,5 +6,12 @@ // https://git-scm.com/docs/gitignore

export const applySpecifierPatternMatching = ({ specifier, url } = {}) => {
export const applySpecifierPatternMatching = ({ specifier, url, ...rest } = {}) => {
assertUrlLike(specifier, "specifier")
assertUrlLike(url, "url")
if (Object.keys(rest).length) {
throw new Error(`received more parameters than expected.
--- name of unexpected parameters ---
${Object.keys(rest)}
--- name of expected parameters ---
specifier, url`)
}
return applyPatternMatching(specifier, url)

@@ -21,4 +28,5 @@ }

while (true) {
// '' === '' -> pass
// pattern consumed and string consumed
if (remainingPattern === "" && remainingString === "") {
// pass because string fully matched pattern
return pass({

@@ -30,4 +38,5 @@ patternIndex,

// '' === value -> fail
// pattern consumed, string not consumed
if (remainingPattern === "" && remainingString !== "") {
// fails because string longer than expected
return fail({

@@ -39,14 +48,14 @@ patternIndex,

// pattern === '' -> pass only if pattern is only **
if (remainingPattern !== "" && remainingString === "") {
// pass because pattern is optionnal
// from this point pattern is not consumed
// string consumed, pattern not consumed
if (remainingString === "") {
// pass because trailing "**" is optional
if (remainingPattern === "**") {
return pass({
patternIndex,
patternIndex: patternIndex + 2,
index,
})
}
// fail because **/ would expect something like /a
// and **a would expect something like foo/bar/a
// fail because string shorted than expected
return fail({

@@ -58,8 +67,31 @@ patternIndex,

if (remainingPattern.slice(0, "**".length) === "**") {
patternIndex += `**`.length
remainingPattern = remainingPattern.slice(`**`.length)
// from this point pattern and string are not consumed
// fast path trailing slash
if (remainingPattern === "/") {
// pass because trailing slash matches remaining
return pass({
patternIndex: patternIndex + 1,
index: string.length,
})
}
// fast path trailing '**'
if (remainingPattern === "**") {
// pass because trailing ** matches remaining
return pass({
patternIndex: patternIndex + 2,
index: string.length,
})
}
// pattern leading **
if (remainingPattern.slice(0, 2) === "**") {
// consumes "**"
remainingPattern = remainingPattern.slice(2)
patternIndex += 2
if (remainingPattern[0] === "/") {
patternIndex += "/".length
remainingPattern = remainingPattern.slice("/".length)
// consumes "/"
remainingPattern = remainingPattern.slice(1)
patternIndex += 1
}

@@ -91,4 +123,5 @@

if (remainingPattern[0] === "*") {
patternIndex += "*".length
remainingPattern = remainingPattern.slice("*".length)
// consumes "*"
remainingPattern = remainingPattern.slice(1)
patternIndex += 1

@@ -146,14 +179,7 @@ // la c'est plus délicat, il faut que remainingString

// trailing slash on pattern, -> match remaining
if (remainingPattern === "/" && remainingString.length > 1) {
return pass({
patternIndex: patternIndex + 1,
index: string.length,
})
}
// consumes next char
remainingPattern = remainingPattern.slice(1)
remainingString = remainingString.slice(1)
patternIndex += 1
index += 1
remainingPattern = remainingPattern.slice(1)
remainingString = remainingString.slice(1)
continue

@@ -191,4 +217,4 @@ }

// search against the next unattempted string
remainingString = remainingString.slice(matchAttempt.index + 1)
index += matchAttempt.index + 1
remainingString = remainingString.slice(matchAttempt.index + 1)
if (remainingString === "") {

@@ -195,0 +221,0 @@ bestMatch = {

import { isPlainObject } from "../isPlainObject.js"
export const metaMapToSpecifierMetaMap = (metaMap) => {
export const metaMapToSpecifierMetaMap = (metaMap, ...rest) => {
if (!isPlainObject(metaMap)) {
throw new TypeError(`metaMap must be a plain object, got ${metaMap}`)
}
if (rest.length) {
throw new Error(`received more arguments than expected.
--- number of arguments received ---
${1 + rest.length}
--- number of arguments expected ---
1`)
}

@@ -8,0 +15,0 @@ const specifierMetaMap = {}

import { assertSpecifierMetaMap } from "../assertSpecifierMetaMap.js"
import { assertUrlLike } from "../assertUrlLike.js"
export const normalizeSpecifierMetaMap = (specifierMetaMap, url) => {
export const normalizeSpecifierMetaMap = (specifierMetaMap, url, ...rest) => {
assertSpecifierMetaMap(specifierMetaMap)
assertUrlLike(url, "url")
if (rest.length) {
throw new Error(`received more arguments than expected.
--- number of arguments received ---
${2 + rest.length}
--- number of arguments expected ---
2`)
}

@@ -6,0 +15,0 @@ const specifierMetaMapNormalized = {}

@@ -5,4 +5,8 @@ import { assertUrlLike } from "../assertUrlLike.js"

export const urlCanContainsMetaMatching = ({ url, specifierMetaMap, predicate }) => {
export const urlCanContainsMetaMatching = ({ url, specifierMetaMap, predicate, ...rest }) => {
assertUrlLike(url, "url")
// the function was meants to be used on url ending with '/'
if (!url.endsWith("/")) {
throw new Error(`url should end with /, got ${url}`)
}
assertSpecifierMetaMap(specifierMetaMap)

@@ -12,8 +16,10 @@ if (typeof predicate !== "function") {

}
if (Object.keys(rest).length) {
throw new Error(`received more parameters than expected.
--- name of unexpected parameters ---
${Object.keys(rest)}
--- name of expected parameters ---
url, specifierMetaMap, predicate`)
}
// we add a trailing slash because we are intested into what will be inside
// this url, not the url itself
// it allows to match pattern for what is inside
const urlWithTrailingSlash = `${url}/`
// for full match we must create an object to allow pattern to override previous ones

@@ -30,3 +36,3 @@ let fullMatchMeta = {}

specifier,
url: urlWithTrailingSlash,
url,
})

@@ -33,0 +39,0 @@ if (matched) {

@@ -5,5 +5,12 @@ import { assertSpecifierMetaMap } from "../assertSpecifierMetaMap.js"

export const urlToMeta = ({ url, specifierMetaMap } = {}) => {
export const urlToMeta = ({ url, specifierMetaMap, ...rest } = {}) => {
assertUrlLike(url)
assertSpecifierMetaMap(specifierMetaMap)
if (Object.keys(rest).length) {
throw new Error(`received more parameters than expected.
--- name of unexpected parameters ---
${Object.keys(rest)}
--- name of expected parameters ---
url, specifierMetaMap`)
}

@@ -15,4 +22,10 @@ return Object.keys(specifierMetaMap).reduce((previousMeta, specifier) => {

})
return matched ? { ...previousMeta, ...specifierMetaMap[specifier] } : previousMeta
if (matched) {
return {
...previousMeta,
...specifierMetaMap[specifier],
}
}
return previousMeta
}, {})
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc