-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
remove
method to HtmlTransformer, useful to downstream Bundle …
…Plugin
- Loading branch information
Showing
5 changed files
with
102 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export function arrayDelete(arr, match) { | ||
if (!Array.isArray(arr)) { | ||
return []; | ||
} | ||
|
||
if (!match) { | ||
return arr; | ||
} | ||
|
||
// only mutates if found | ||
if (typeof match === "function") { | ||
if (arr.find(match)) { | ||
return arr.filter((entry) => { | ||
return !match(entry); | ||
}); | ||
} | ||
} else if (arr.includes(match)) { | ||
return arr.filter((entry) => { | ||
return entry !== match; | ||
}); | ||
} | ||
|
||
return arr; | ||
} |
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,53 @@ | ||
import test from "ava"; | ||
import {arrayDelete} from "../src/Util/ArrayUtil.js"; | ||
|
||
test("ArrayUtil.arrayDelete empties", async (t) => { | ||
t.deepEqual(arrayDelete(), []); | ||
t.deepEqual(arrayDelete(undefined, 1), []); | ||
|
||
t.deepEqual(arrayDelete(null), []); | ||
t.deepEqual(arrayDelete(1), []); | ||
t.deepEqual(arrayDelete(true), []); | ||
t.deepEqual(arrayDelete(false), []); | ||
}); | ||
|
||
test("ArrayUtil.arrayDelete if array does not have value, it does not mutate", async (t) => { | ||
let empty = []; | ||
t.is(arrayDelete(empty), empty); | ||
t.is(arrayDelete(empty, 1), empty); | ||
t.is(arrayDelete(empty, true), empty); | ||
t.is(arrayDelete(empty, undefined), empty); | ||
}); | ||
|
||
test("ArrayUtil.arrayDelete if array does not have function matched value, it does not mutate", async (t) => { | ||
let empty = []; | ||
t.is(arrayDelete(empty, () => false), empty); | ||
}); | ||
|
||
|
||
test("ArrayUtil.arrayDelete mutates when array contains match", async (t) => { | ||
let a = [1, 2]; | ||
t.not(arrayDelete(a, 1), [2]); | ||
t.deepEqual(arrayDelete(a, 1), [2]); | ||
}); | ||
|
||
test("ArrayUtil.arrayDelete mutates when array contains function matched value", async (t) => { | ||
let a = [1, 2]; | ||
t.not(arrayDelete(a, entry => entry === 1), [2]); | ||
t.deepEqual(arrayDelete(a, entry => entry === 1), [2]); | ||
}); | ||
|
||
test("ArrayUtil.arrayDelete complex delete", async (t) => { | ||
let a = [1,2,3,4,5,6,7,8]; | ||
t.deepEqual(arrayDelete(a, 4), [1,2,3,5,6,7,8]); | ||
}); | ||
|
||
test("ArrayUtil.arrayDelete function matched delete", async (t) => { | ||
let a = [1,2,3,4,5,6,7,8]; | ||
t.deepEqual(arrayDelete(a, entry => entry === 4), [1,2,3,5,6,7,8]); | ||
}); | ||
|
||
test("ArrayUtil.arrayDelete double delete", async (t) => { | ||
let a = [1,2,3,4,5,6,7,8]; | ||
t.deepEqual(arrayDelete(arrayDelete(a, 4), 6), [1,2,3,5,7,8]); | ||
}); |