Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 8, add TypeScript definition #12

Merged
merged 2 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
51 changes: 51 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
Escape a string for use in HTML.

Escapes the following characters in the given `string` string: `&` `<` `>` `"` `'`.
BendingBender marked this conversation as resolved.
Show resolved Hide resolved

@example
```
import * as escapeGoat from 'escape-goat';

escapeGoat.escape('🦄 & 🐐');
//=> '🦄 &amp; 🐐'

escapeGoat.escape('Hello <em>World</em>');
//=> 'Hello &lt;em&gt;World&lt;/em&gt;'
```
*/
export function escape(string: string): string;

/**
Unescape an HTML string to use as a plain string.

Unescapes the following HTML entities in the given `htmlString` string: `&amp;` `&lt;` `&gt;` `&quot;` `&#39;`.
BendingBender marked this conversation as resolved.
Show resolved Hide resolved

@example
```
import * as escapeGoat from 'escape-goat';
BendingBender marked this conversation as resolved.
Show resolved Hide resolved

escapeGoat.unescape('🦄 &amp; 🐐');
//=> '🦄 & 🐐'
```
*/
export function unescape(htmlString: string): string;

/**
[Tagged template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) that escapes interpolated values.

@example
```
import * as escapeGoat from 'escape-goat';

const url = 'https://sindresorhus.com?x="🦄"';
escapeGoat.escapeTag`<a href="${url}">Unicorn</a>`;
//=> '<a href="https://sindresorhus.com?x=&quot;🦄&quot;">Unicorn</a>'
```
*/
export function escapeTag(template: TemplateStringsArray, ...substitutions: unknown[]): string;
BendingBender marked this conversation as resolved.
Show resolved Hide resolved

/**
[Tagged template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) that unescapes interpolated values.
*/
BendingBender marked this conversation as resolved.
Show resolved Hide resolved
export function unescapeTag(template: TemplateStringsArray, ...substitutions: unknown[]): string;
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
'use strict';

exports.escape = input => input
exports.escape = string => string
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');

exports.unescape = input => input
exports.unescape = htmlString => htmlString
.replace(/&gt;/g, '>')
.replace(/&lt;/g, '<')
.replace(/&#39;/g, '\'')
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&');

exports.escapeTag = function (input) {
let output = input[0];
for (let i = 1; i < arguments.length; i++) {
output = output + exports.escape(arguments[i]) + input[i];
exports.escapeTag = (strings, ...values) => {
let output = strings[0];
for (let i = 0; i < values.length; i++) {
output = output + exports.escape(values[i]) + strings[i + 1];
}

return output;
};

exports.unescapeTag = function (input) {
let output = input[0];
for (let i = 1; i < arguments.length; i++) {
output = output + exports.unescape(arguments[i]) + input[i];
exports.unescapeTag = (strings, ...values) => {
let output = strings[0];
for (let i = 0; i < values.length; i++) {
output = output + exports.unescape(values[i]) + strings[i + 1];
}

return output;
};
10 changes: 10 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expectType} from 'tsd';
import * as escapeGoat from '.';

expectType<string>(escapeGoat.escape('🦄 & 🐐'));
expectType<string>(escapeGoat.unescape('🦄 &amp; 🐐'));
expectType<string>(escapeGoat.escape('Hello <em>World</em>'));
const url = 'https://sindresorhus.com?x="🦄"';
expectType<string>(escapeGoat.escapeTag`<a href="${url}">Unicorn</a>`);
const escaped = '🦄 &amp; 🐐';
expectType<string>(escapeGoat.unescapeTag`unicorn and goat: ${escaped}`);
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:
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 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.
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.
84 changes: 43 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
{
"name": "escape-goat",
"version": "1.3.0",
"description": "Escape a string for use in HTML or the inverse",
"license": "MIT",
"repository": "sindresorhus/escape-goat",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"escape",
"unescape",
"html",
"entity",
"entities",
"escaping",
"sanitize",
"sanitization",
"utility",
"template",
"attribute",
"value",
"interpolate",
"xss",
"goat",
"🐐"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "escape-goat",
"version": "1.3.0",
"description": "Escape a string for use in HTML or the inverse",
"license": "MIT",
"repository": "sindresorhus/escape-goat",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"escape",
"unescape",
"html",
"entity",
"entities",
"escaping",
"sanitize",
"sanitization",
"utility",
"template",
"attribute",
"value",
"interpolate",
"xss",
"goat",
"🐐"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ escapeGoat.escapeTag`<a href="${url}">Unicorn</a>`;

## API

### escapeGoat.escape(input)
### escapeGoat.escape(string)

Escapes the following characters in the given `input` string: `&` `<` `>` `"` `'`
Escapes the following characters in the given `string` string: `&` `<` `>` `"` `'`

### escapeGoat.unescape(input)
### escapeGoat.unescape(htmlString)

Unescapes the following HTML entities in the given `input` string: `&amp;` `&lt;` `&gt;` `&quot;` `&#39;`
Unescapes the following HTML entities in the given `htmlString` string: `&amp;` `&lt;` `&gt;` `&quot;` `&#39;`

### escapeGoat.escapeTag

Expand Down
32 changes: 16 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import test from 'ava';
import m from '.';
import escapeGoat from '.';

test('escape', t => {
t.is(m.escape('&<>"\''), '&amp;&lt;&gt;&quot;&#39;');
t.is(m.escape('🦄 & 🐐'), '🦄 &amp; 🐐');
t.is(m.escape('Hello <em>World</em>'), 'Hello &lt;em&gt;World&lt;/em&gt;');
t.is(escapeGoat.escape('&<>"\''), '&amp;&lt;&gt;&quot;&#39;');
t.is(escapeGoat.escape('🦄 & 🐐'), '🦄 &amp; 🐐');
t.is(escapeGoat.escape('Hello <em>World</em>'), 'Hello &lt;em&gt;World&lt;/em&gt;');
});

test('unescape', t => {
t.is(m.unescape('&amp;&lt;&gt;&quot;&#39;'), '&<>"\'');
t.is(m.unescape('🦄 &amp; 🐐'), '🦄 & 🐐');
t.is(m.unescape('Hello &lt;em&gt;World&lt;/em&gt;'), 'Hello <em>World</em>');
t.is(escapeGoat.unescape('&amp;&lt;&gt;&quot;&#39;'), '&<>"\'');
t.is(escapeGoat.unescape('🦄 &amp; 🐐'), '🦄 & 🐐');
t.is(escapeGoat.unescape('Hello &lt;em&gt;World&lt;/em&gt;'), 'Hello <em>World</em>');
});

test('escape & unescape', t => {
t.is(m.unescape(m.escape('&<>"\'')), '&<>"\'');
t.is(m.unescape(m.escape('&quot;')), '&quot;');
t.is(escapeGoat.unescape(escapeGoat.escape('&<>"\'')), '&<>"\'');
t.is(escapeGoat.unescape(escapeGoat.escape('&quot;')), '&quot;');
});

test('escapeTag', t => {
t.is(m.escapeTag`foobarz${`&<>"'`}`, 'foobarz&amp;&lt;&gt;&quot;&#39;');
t.is(m.escapeTag`🦄 ${'&'} 🐐`, '🦄 &amp; 🐐');
t.is(m.escapeTag`Hello <em><>${`<>`}</em>`, 'Hello <em><>&lt;&gt;</em>');
t.is(escapeGoat.escapeTag`foobarz${'&<>"\''}`, 'foobarz&amp;&lt;&gt;&quot;&#39;');
t.is(escapeGoat.escapeTag`🦄 ${'&'} 🐐`, '🦄 &amp; 🐐');
t.is(escapeGoat.escapeTag`Hello <em><>${'<>'}</em>`, 'Hello <em><>&lt;&gt;</em>');
});

test('unescapeTag', t => {
t.is(m.unescapeTag`foobarz${'&amp;&lt;&gt;&quot;&#39;'}`, 'foobarz&<>"\'');
t.is(m.unescapeTag`🦄 ${'&amp;'} 🐐`, '🦄 & 🐐');
t.is(m.unescapeTag`Hello <em><>${`&lt;&gt;`}</em>`, 'Hello <em><><></em>');
t.is(escapeGoat.unescapeTag`foobarz${'&amp;&lt;&gt;&quot;&#39;'}`, 'foobarz&<>"\'');
t.is(escapeGoat.unescapeTag`🦄 ${'&amp;'} 🐐`, '🦄 & 🐐');
t.is(escapeGoat.unescapeTag`Hello <em><>${'&lt;&gt;'}</em>`, 'Hello <em><><></em>');
});

test('escapeTag & unescapeTag', t => {
const input = '&<>"\'';
const actual = m.unescapeTag`${m.escapeTag`${input}`}`;
const actual = escapeGoat.unescapeTag`${escapeGoat.escapeTag`${input}`}`;
t.is(actual, input);
});