Here’s that as a snippet:
#popclip - remove duplicated words, attempt 1
name: Remove Duplicates
icon: square dup
after: paste-result
javascript: |
const regex = /\b(\w+)(?:\W+\1\b)+/g
return popclip.input.text.replace(regex, (_, word) => word)
(The above block is an extension snippet - select it to install the extension with PopClip)
Pretty good but it only removes duplicates that are adjacent, so: one one two two
→ one two
,
but: one two two one
→ one two one
Let me try another approach:
#popclip - remove duplicated words attempt 2
name: Remove Duplicates
icon: square dup
after: paste-result
javascript: |
const words = popclip.input.text.split(/\s/)
return [...new Set(words)].join(' ')
// technique thanks to <https://stackoverflow.com/a/14438954/220847>
// also note: "iteration of sets visits items in insertion order" (MDN)
This time: one two two one
→ one two