Skip to content

Instantly share code, notes, and snippets.

@irrationalistic
Created July 15, 2015 18:12
Show Gist options
  • Save irrationalistic/656b44c10f95ba621be4 to your computer and use it in GitHub Desktop.
Save irrationalistic/656b44c10f95ba621be4 to your computer and use it in GitHub Desktop.
UltiSnips: Require Snippet for Javascript
global !p
def camel(line):
if not line: return ''
res = re.sub(r'[_-].?',lambda x: x.group()[1].upper() if len(x.group()) > 1 else '' ,line)
res = res.replace('.', '')
if not res: return ''
return res[0].lower() + res[1:]
def filename(path):
return os.path.splitext(os.path.basename(path))[0]
def firstUpper(line):
if not line: return ''
return line[0].upper() + line[1:]
endglobal
snippet req "CommonJS Require" b
var ${2:`!p snip.rv = camel(filename(t[1]))`} = require('$1');
endsnippet
snippet reqt "CommonJS Require Title Case" b
var ${2:`!p snip.rv = firstUpper(camel(filename(t[1])))`} = require('$1');
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment