|
| 1 | +// Sass. |
| 2 | +@use "sass:list"; |
| 3 | + |
| 4 | +// Completed |
| 5 | +// The `list.remove-duplicate()` or `remove.duplicate()` function returns the list without duplicate values. |
| 6 | +// @param `$list` The list from which duplicate values are being removed. |
| 7 | +// @arbitrary `$values...` Arbitrary list of elements to remove from `$list`. |
| 8 | +// @returns The returned value is a copy of `$list` with unique elements. |
| 9 | +@function duplicate($list, $values...) { |
| 10 | + $result: (); |
| 11 | + @for $i from 1 through list.length($list) { |
| 12 | + @if list.length($values) > |
| 13 | + 0 and not |
| 14 | + list.index($values, list.nth($list, $i)) or not |
| 15 | + list.index($result, list.nth($list, $i)) |
| 16 | + { |
| 17 | + $result: list.append($result, list.nth($list, $i), list.separator($list)); |
| 18 | + } |
| 19 | + } |
| 20 | + @return $result; |
| 21 | +} |
| 22 | + |
| 23 | +// Examples. |
| 24 | +// SECTION: $remove not defined |
| 25 | +// @debug duplicate(('a', 'b', 'c', ('d', 'e', 'f'), 'g', 'h', 'i', 'j', 'k')); // "a", "b", "c", ("d", "e", "f"), "g", "h", "i", "j", "k" |
| 26 | + |
| 27 | +// string |
| 28 | +// @debug duplicate(('a', 'b', 'a', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k')); // "a", "b", "c", ("d", "e", "f"), "h", "g", "i", "j", "k" |
| 29 | + |
| 30 | +// map |
| 31 | +// @debug duplicate(((1: 1), (2: 2), (3, 3), (1: 1), (3, 3))); // (1: 1), (2: 2), (3, 3) |
| 32 | + |
| 33 | +// number |
| 34 | +// @debug duplicate((0.9, 1, 1.1, 1, 2, 'a', 2, 'b', 3, 3, 4, 5, 6, 7, 8)); // 0.9, 1, 1.1, 2, "a", "b", 3, 4, 5, 6, 7, 8 |
| 35 | +// @debug duplicate((a, b, 1, true, false, a, c, 1, c, (d, e, f), h, g, true, h, i, j, k), 1); // a, b, 1, true, false, a, c, c, (d, e, f), h, g, true, h, i, j, k |
| 36 | +// @debug duplicate((a, b, 1, true, 3.14, a, c, 1, c, (d, e, f), h, g, true, 3.14, h, i, j, k), 1, 3.14); // a, b, 1, true, 3.14, a, c, c, (d, e, f), h, g, true, h, i, j, k |
| 37 | + |
| 38 | +// list |
| 39 | +// @debug duplicate(( (1, 1), (2, 2), (3, 3), (1, 1), (3, 3) )); // (1, 1), (2, 2), (3, 3) |
| 40 | +// @debug duplicate(('a', primary dark, 'a', 'c', primary dark, ('d', 'e', 'f'), 'h', 'g', 'h', ('d', 'e', 'f'), 'i', 'j', 'k'), primary dark, ('d', 'e', 'f')); // "a", primary dark, "a", "c", ("d", "e", "f"), "h", "g", "h", "i", "j", "k" |
| 41 | + |
| 42 | +// SECTION: $remove defined |
| 43 | +// remove specific duplicates |
| 44 | +// @debug duplicate(('a', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'c'); // "a", "b", "a", ("d", "e", "f"), "h", "g", "h", "i", "j", "k" |
| 45 | +// @debug duplicate(('a', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'c', 'a'); // "b", ("d", "e", "f"), "h", "g", "h", "i", "j", "k" |
| 46 | + |
| 47 | +// string |
| 48 | +// @debug duplicate(('a', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'c'); // "a", "b", "a", "c", ("d", "e", "f"), "h", "g", "h", "i", "j", "k" |
| 49 | +// @debug duplicate(('a', 'k', 'b', 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', 'h', 'i', 'j', 'k'), 'k'); // "a", "k", "b", "a", "c", "c", ("d", "e", "f"), "h", "g", "h", "i", "j" |
| 50 | + |
| 51 | +// bool |
| 52 | +// @debug duplicate(('a', 'b', true, false, 'a', 'c', 'c', ('d', 'e', 'f'), 'h', 'g', true, 'h', 'i', 'j', 'k'), true); // "a", "b", true, false, "a", "c", "c", ("d", "e", "f"), "h", "g", "h", "i", "j", "k" |
| 53 | + |
| 54 | +// null |
| 55 | +// @debug duplicate(((1, 1), null, (2, 2), (3, 3), (1, 1), null, (3, 3) )); // (1, 1), null, (2, 2), (3, 3) |
| 56 | + |
| 57 | +// list |
| 58 | +// @debug duplicate(((1, 1), null, (2, 2), (3, 3), (1, 1), null, (3, 3) ), (3, 3)); // (1, 1), null, (2, 2), (3, 3), (1, 1), null |
0 commit comments