Skip to content

Commit

Permalink
adds optional spacer argument to strip
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Nov 7, 2023
1 parent f9da709 commit 09799b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/strip.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ const diacritics = [
@function strip
@desc Removes all non ASCII characters from a string.
@param {String} value
@param {String} [spacer = "-"] The character(s) to be used in place of spaces.
*/
export default function(value) {
export default function(value, spacer = "-") {

return `${value}`.replace(/[^A-Za-z0-9\-_\u0621-\u064A]/g, char => {

if (char === " ") return "-";
if (char === " ") return spacer;

let ret = false;
for (let d = 0; d < diacritics.length; d++) {
Expand Down
2 changes: 1 addition & 1 deletion test/strip-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {default as strip} from "../src/strip.js";
it("strip", () => {

assert.strictEqual(strip("one two"), "one-two", "Strips Spaces");
assert.strictEqual(strip("one two", " "), "one-two", "Changes Spacer Argument");
assert.strictEqual(strip("one two", " "), "one two", "Changes Spacer Argument");
assert.strictEqual(strip("one@two"), "onetwo", "Strips Non-character");
assert.strictEqual(strip("á"), "a", "Strips Diacritic");
assert.strictEqual(strip("والاجتماعية"), "والاجتماعية", "Keeps Arabic");
Expand Down

0 comments on commit 09799b8

Please sign in to comment.