Skip to content

Instantly share code, notes, and snippets.

@felixrabe
Last active September 29, 2023 16:39
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixrabe/db88674566e14e413c6f to your computer and use it in GitHub Desktop.
Save felixrabe/db88674566e14e413c6f to your computer and use it in GitHub Desktop.
CoffeeScript: String.startsWith() and String.endsWith()
# http://stackoverflow.com/a/646643
String::startsWith ?= (s) -> @slice(0, s.length) == s
String::endsWith ?= (s) -> s == '' or @slice(-s.length) == s
@mirek
Copy link

mirek commented Oct 1, 2014

String::startsWith ?= (s) -> @[...s.length] is s
String::endsWith   ?= (s) -> s is '' or @[-s.length..] is s

@billiegoose
Copy link

This is so beautiful.

@Obiwarn
Copy link

Obiwarn commented Feb 9, 2016

Jesus. Indeed.

@Michael-F-Ellis
Copy link

Verily!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment