Update slug sanitization errors.
This commit is contained in:
parent
ddcf009fc6
commit
b8006bbcbe
@ -55,24 +55,24 @@ describe('sanitizeIRI', () => {
|
||||
describe('sanitizeSlug', ()=> {
|
||||
|
||||
it('throws an error for non-strings', () => {
|
||||
expect(() => sanitizeSlug({})).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug([])).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug(false)).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug(null)).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug(11234)).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug(undefined)).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug(()=>{})).toThrowError("`sanitizeSlug` only accepts strings as input.");
|
||||
expect(() => sanitizeSlug({})).toThrowError("The input slug must be a string.");
|
||||
expect(() => sanitizeSlug([])).toThrowError("The input slug must be a string.");
|
||||
expect(() => sanitizeSlug(false)).toThrowError("The input slug must be a string.");
|
||||
expect(() => sanitizeSlug(null)).toThrowError("The input slug must be a string.");
|
||||
expect(() => sanitizeSlug(11234)).toThrowError("The input slug must be a string.");
|
||||
expect(() => sanitizeSlug(undefined)).toThrowError("The input slug must be a string.");
|
||||
expect(() => sanitizeSlug(()=>{})).toThrowError("The input slug must be a string.");
|
||||
});
|
||||
|
||||
it('throws an error for non-string replacements', () => {
|
||||
expect(() => sanitizeSlug('test', { replacement: {} })).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: [] })).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: false })).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: null } )).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: 11232 })).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: {} })).toThrowError("`options.replacement` must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: [] })).toThrowError("`options.replacement` must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: false })).toThrowError("`options.replacement` must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: null } )).toThrowError("`options.replacement` must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: 11232 })).toThrowError("`options.replacement` must be a string.");
|
||||
// do not test undefined for this variant since a default is set in the cosntructor.
|
||||
//expect(() => sanitizeSlug('test', { replacement: undefined })).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: ()=>{} })).toThrowError("the `sanitizeSlug` replacement character must be a string.");
|
||||
//expect(() => sanitizeSlug('test', { replacement: undefined })).toThrowError("`options.replacement` must be a string.");
|
||||
expect(() => sanitizeSlug('test', { replacement: ()=>{} })).toThrowError("`options.replacement` must be a string.");
|
||||
});
|
||||
|
||||
it('removes double replacements', () => {
|
||||
|
@ -25,10 +25,12 @@ const uriChars = /[\w\-.~]/i;
|
||||
const ucsChars = /[\xA0-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFEF}\u{10000}-\u{1FFFD}\u{20000}-\u{2FFFD}\u{30000}-\u{3FFFD}\u{40000}-\u{4FFFD}\u{50000}-\u{5FFFD}\u{60000}-\u{6FFFD}\u{70000}-\u{7FFFD}\u{80000}-\u{8FFFD}\u{90000}-\u{9FFFD}\u{A0000}-\u{AFFFD}\u{B0000}-\u{BFFFD}\u{C0000}-\u{CFFFD}\u{D0000}-\u{DFFFD}\u{E1000}-\u{EFFFD}]/u;
|
||||
// `sanitizeIRI` does not actually URI-encode the chars (that is the browser's and server's job), just removes the ones that are not allowed.
|
||||
export function sanitizeIRI(str, { replacement = "" } = {}) {
|
||||
if (!isString(replacement)) throw "`options.replacement` must be a string.";
|
||||
if (replacement !== "") {
|
||||
const validReplacement = (sanitizeIRI(replacement) === replacement);
|
||||
if (!validReplacement) throw "The replacement character(s) for `sanitizeIRI` is itself unsafe.";
|
||||
if (!validReplacement) throw "The replacement character(s) (options.replacement) is itself unsafe.";
|
||||
}
|
||||
|
||||
let result = "";
|
||||
// We cannot use a `map` function here because `string.split()`
|
||||
// splits things like emojis into UTF-16 surrogate pairs,
|
||||
@ -44,8 +46,8 @@ export function sanitizeIRI(str, { replacement = "" } = {}) {
|
||||
}
|
||||
|
||||
export function sanitizeSlug(str, { replacement = '-' } = {}) {
|
||||
if (!isString(str)) throw "`sanitizeSlug` only accepts strings as input.";
|
||||
if (!isString(replacement)) throw "the `sanitizeSlug` replacement character must be a string.";
|
||||
if (!isString(str)) throw "The input slug must be a string.";
|
||||
if (!isString(replacement)) throw "`options.replacement` must be a string.";
|
||||
|
||||
// Sanitize as IRI (i18n URI) and as filename.
|
||||
const sanitize = flow([
|
||||
|
Loading…
x
Reference in New Issue
Block a user