This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

53
node_modules/pupa/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,53 @@
export class MissingValueError extends Error {
name: 'MissingValueError';
message: string;
key: string;
constructor(key: string);
}
export type Options = {
/**
By default, Pupa throws a `MissingValueError` when a placeholder resolves to `undefined`. With this option set to `true`, it simply ignores it and leaves the placeholder as is.
@default false
*/
ignoreMissing?: boolean;
/**
Performs arbitrary operation for each interpolation. If the returned value was `undefined`, it behaves differently depending on the `ignoreMissing` option. Otherwise, the returned value will be interpolated into a string (and escaped when double-braced) and embedded into the template.
@default ({value}) => value
*/
transform?: (data: {value: unknown; key: string}) => unknown;
};
/**
Simple micro templating.
@param template - Text with placeholders for `data` properties.
@param data - Data to interpolate into `template`.
@example
```
import pupa from 'pupa';
pupa('The mobile number of {name} is {phone.mobile}', {
name: 'Sindre',
phone: {
mobile: '609 24 363'
}
});
//=> 'The mobile number of Sindre is 609 24 363'
pupa('I like {0} and {1}', ['🦄', '🐮']);
//=> 'I like 🦄 and 🐮'
// Double braces encodes the HTML entities to avoid code injection.
pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
//=> 'I like &lt;br&gt;🦄&lt;/br&gt; and &lt;i&gt;🐮&lt;/i&gt;'
```
*/
export default function pupa(
template: string,
data: unknown[] | Record<string, any>,
options?: Options
): string;

50
node_modules/pupa/index.js generated vendored Normal file
View File

@@ -0,0 +1,50 @@
import {htmlEscape} from 'escape-goat';
export class MissingValueError extends Error {
constructor(key) {
super(`Missing a value for ${key ? `the placeholder: ${key}` : 'a placeholder'}`, key);
this.name = 'MissingValueError';
this.key = key;
}
}
export default function pupa(template, data, {ignoreMissing = false, transform = ({value}) => value} = {}) {
if (typeof template !== 'string') {
throw new TypeError(`Expected a \`string\` in the first argument, got \`${typeof template}\``);
}
if (typeof data !== 'object') {
throw new TypeError(`Expected an \`object\` or \`Array\` in the second argument, got \`${typeof data}\``);
}
const replace = (placeholder, key) => {
let value = data;
for (const property of key.split('.')) {
value = value ? value[property] : undefined;
}
const transformedValue = transform({value, key});
if (transformedValue === undefined) {
if (ignoreMissing) {
return placeholder;
}
throw new MissingValueError(key);
}
return String(transformedValue);
};
const composeHtmlEscape = replacer => (...args) => htmlEscape(replacer(...args));
// The regex tries to match either a number inside `{{ }}` or a valid JS identifier or key path.
const doubleBraceRegex = /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}/gi;
if (doubleBraceRegex.test(template)) {
template = template.replace(doubleBraceRegex, composeHtmlEscape(replace));
}
const braceRegex = /{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi;
return template.replace(braceRegex, replace);
}

9
node_modules/pupa/license generated vendored Normal file
View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
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.

51
node_modules/pupa/package.json generated vendored Normal file
View File

@@ -0,0 +1,51 @@
{
"name": "pupa",
"version": "3.1.0",
"description": "Simple micro templating",
"license": "MIT",
"repository": "sindresorhus/pupa",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"string",
"formatting",
"template",
"object",
"format",
"interpolate",
"interpolation",
"templating",
"expand",
"simple",
"replace",
"placeholders",
"values",
"transform",
"micro"
],
"dependencies": {
"escape-goat": "^4.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.17.0",
"typescript": "^4.3.5",
"xo": "^0.41.0"
}
}

79
node_modules/pupa/readme.md generated vendored Normal file
View File

@@ -0,0 +1,79 @@
# pupa
> Simple micro templating
Useful when all you need is to fill in some placeholders.
## Install
```
$ npm install pupa
```
## Usage
```js
import pupa from 'pupa';
pupa('The mobile number of {name} is {phone.mobile}', {
name: 'Sindre',
phone: {
mobile: '609 24 363'
}
});
//=> 'The mobile number of Sindre is 609 24 363'
pupa('I like {0} and {1}', ['🦄', '🐮']);
//=> 'I like 🦄 and 🐮'
// Double braces encodes the HTML entities to avoid code injection.
pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
//=> 'I like &lt;br&gt;🦄&lt;/br&gt; and &lt;i&gt;🐮&lt;/i&gt;'
```
## API
### pupa(template, data, options?)
#### template
Type: `string`
Text with placeholders for `data` properties.
#### data
Type: `object | unknown[]`
Data to interpolate into `template`.
#### options
Type: `object`
##### ignoreMissing
Type: `boolean`\
Default: `false`
By default, Pupa throws a `MissingValueError` when a placeholder resolves to `undefined`. With this option set to `true`, it simply ignores it and leaves the placeholder as is.
##### transform
Type: `((data: {value: unknown; key: string}) => unknown) | undefined` (default: `({value}) => value`)
Performs arbitrary operation for each interpolation. If the returned value was `undefined`, it behaves differently depending on the `ignoreMissing` option. Otherwise, the returned value will be interpolated into a string (and escaped when double-braced) and embedded into the template.
### MissingValueError
Exposed for instance checking.
## FAQ
### What about template literals?
Template literals expand on creation. This module expands the template on execution, which can be useful if either or both template and data are lazily created or user-supplied.
## Related
- [pupa-cli](https://github.com/sindresorhus/pupa-cli) - CLI for this module