Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@smt
Created August 19, 2015 21: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 smt/0c1dc82a4dfa9736003b to your computer and use it in GitHub Desktop.
Save smt/0c1dc82a4dfa9736003b to your computer and use it in GitHub Desktop.
Dynamic filename-based names in JS snippets (using UltiSnips)
# Use the basename (of the file in which the snippet is invoked) as a sensible
# default for module names. Supports JSX files as well.
global !p
def module_name(name=None):
name = name or 'ModuleName'
s1 = re.sub('\.js(x?)$', '', name)
return ''.join(x.title() for x in re.sub('([A-Z])', r'_\1', s1).split('_'))
endglobal
# Usage:
# In file myCleverApp.js, expands to `module.exports = MyCleverApp;`
snippet me "module.exports" b
module.exports = ${1:`!p snip.rv = module_name(snip.basename)`};$0
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment