Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@owenandrews
Created May 31, 2022 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owenandrews/d2c2a5b0dc8681e1fb774ed27f8f29c1 to your computer and use it in GitHub Desktop.
Save owenandrews/d2c2a5b0dc8681e1fb774ed27f8f29c1 to your computer and use it in GitHub Desktop.
/*
From Cassidy Williams' weekly newsletter (https://click.pstmrk.it/2sm/buttondown.email%2Fcassidoo%2Fsubscribers%2F4d5ba807-3668-4d77-9db5-4e3303c0f3a9%2Farchive%2Fkeep-your-face-always-toward-the-sunshine-and/WPtx8C0N/bP1H/IfMBgFIdr5/OGIyZDRjMjctMTAzMy00Y2Y2LWI2YWMtZjVlNTRiOWFmNDA1)
Write a function that determines if all the characters in a given string are unique. Can you do this without making any new variables? You choose if you want to include capitalization in your consideration for this one, as a fun challenge.
Example:
> allUnique('Cassidy')
> false
> allUnique('cat & dog')
> false
> allUnique('cat+dog')
> true
*/
export const allUnique = (chars, ignoreCase) => !RegExp(/([\s\S])[\s\S]*(?=\1)/, ignoreCase ? ['i'] : []).test(chars);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment