-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compat): implement unescape (#874)
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { escape } from './escape'; | ||
import { unescape } from './unescape'; | ||
import { map } from '../array/map'; | ||
import { stubString } from '../util/stubString'; | ||
|
||
describe('unescape', () => { | ||
let escaped = '&<>"'/'; | ||
let unescaped = '&<>"\'/'; | ||
|
||
escaped += escaped; | ||
unescaped += unescaped; | ||
|
||
it('should unescape entities in order', () => { | ||
expect(unescape('&lt;')).toBe('<'); | ||
}); | ||
|
||
it('should unescape the proper entities', () => { | ||
expect(unescape(escaped)).toBe(unescaped); | ||
}); | ||
|
||
it('should handle strings with nothing to unescape', () => { | ||
expect(unescape('abc')).toBe('abc'); | ||
}); | ||
|
||
it('should unescape the same characters escaped by `_.escape`', () => { | ||
expect(unescape(escape(unescaped))).toBe(unescaped); | ||
}); | ||
|
||
it('should handle leading zeros in html entities', () => { | ||
expect(unescape(''')).toBe("'"); | ||
expect(unescape(''')).toBe("'"); | ||
expect(unescape(''')).toBe("'"); | ||
}); | ||
|
||
['`', '/'].forEach(entity => { | ||
it(`should not unescape the "${entity}" entity`, () => { | ||
expect(unescape(entity)).toBe(entity); | ||
}); | ||
}); | ||
|
||
it('should return an empty string for empty values', () => { | ||
// eslint-disable-next-line no-sparse-arrays | ||
const values = [, null, undefined, '']; | ||
const expected = map(values, stubString); | ||
|
||
const actual = map(values, (value, index) => (index ? unescape(value as any) : unescape())); | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { unescape as unescapeToolkit } from '../../string/unescape.ts'; | ||
import { toString } from '../util/toString.ts'; | ||
|
||
/** | ||
* Converts the HTML entities `&`, `<`, `>`, `"`, and `'` in `str` to their corresponding characters. | ||
* It is the inverse of `escape`. | ||
* | ||
* @param {string} str The string to unescape. | ||
* @returns {string} Returns the unescaped string. | ||
* | ||
* @example | ||
* unescape('This is a <div> element.'); // returns 'This is a <div> element.' | ||
* unescape('This is a "quote"'); // returns 'This is a "quote"' | ||
* unescape('This is a 'quote''); // returns 'This is a 'quote'' | ||
* unescape('This is a & symbol'); // returns 'This is a & symbol' | ||
*/ | ||
export function unescape(str?: string): string { | ||
return unescapeToolkit(toString(str)); | ||
} |