Skip to content

Instantly share code, notes, and snippets.

@repeatingbeats
Last active December 14, 2015 11:39
Show Gist options
  • Save repeatingbeats/5080400 to your computer and use it in GitHub Desktop.
Save repeatingbeats/5080400 to your computer and use it in GitHub Desktop.
Vim + UltiSnips + UMD

Pretty Examples


Keystrokes:

umdp<Tab>foo

Output:

(function (root, factory) {                                                      
                                                                                 
  // Node                                                                    
  if (typeof exports === 'object') {                                             
    module.exports = factory();                                                  
  }                                                                              
                                                                                 
  // AMD                                                                         
  else if (typeof define === 'function' && define.amd) {                         
    define(factory);                                                             
  }                                                                              
                                                                                 
  // Export to global                                                            
  else {                                                                         
    root.foo = factory();                                                        
  }                                                                              
                                                                                 
}(this, function () {                                                            
                                                                                 
  // Exported module API                                                         
  return {};                                                                      
                                                                                 
}));

Keystrokes:

umdp<Tab>foo<C-j>bar

Output:

(function (root, factory) {                                                      
                                                                                 
  // Node                                                                    
  if (typeof exports === 'object') {                                             
    module.exports = factory(                                                    
      require('bar')                                                             
    );                                                                           
  }                                                                              
                                                                                 
  // AMD                                                                         
  else if (typeof define === 'function' && define.amd) {                         
    define([                                                                     
      'bar'                                                                      
    ], factory);                                                                 
  }                                                                              
                                                                                 
  // Export to global                                                            
  else {                                                                         
    root.foo = factory(                                                          
      root.bar                                                                   
    );                                                                           
  }                                                                              
                                                                                 
}(this, function (bar) {                                                         
                                                                                 
  // Exported module API                                                         
  return {};                                                                      
                                                                                 
}));

Keystrokes:

umdp<Tab>foo<C-j>bar, baz, bam

Output:

(function (root, factory) {

  // Node
  if (typeof exports === 'object') {
    module.exports = factory(
      require('bar')
    , require('baz')
    , require('bam')
    );
  }

  // AMD
  else if (typeof define === 'function' && define.amd) {
    define([
      'bar'
    , 'baz'
    , 'bam'
    ], factory);
  }

  // Export to global
  else {
    root.foo = factory(
      root.bar
    , root.baz
    , root.bam
    );
  }

}(this, function (bar, baz, bam) {

  // Exported module API
  return {};

}));

Terse Examples


Keystrokes:

umd<Tab>foo

Output:

(function (root, factory) {                                                      
  if (typeof exports === 'object') module.exports = factory();                      
  else if (typeof define === 'function' && define.amd) define(factory);             
  else root.foo = factory();                                                        
}(this, function () {                                                               
  return {};                                                                         
}));

Keystrokes:

umd<Tab>foo<C-j>bar

Output:

(function (root, factory) {                                                      
  if (typeof exports === 'object') module.exports = factory(require('bar'));     
  else if (typeof define === 'function' && define.amd) define([ 'bar' ], factory);
  else root.foo = factory(root.bar);                                             
}(this, function (bar) {                                                         
  return {};                                                                      
}));

Keystrokes:

umd<Tab>foo<C-j>bar, baz, bam

Output:

(function (root, factory) {                                                      
  if (typeof exports === 'object') module.exports = factory(require('bar'), require('baz'), require('bam'));
  else if (typeof define === 'function' && define.amd) define([ 'bar', 'baz', 'bam' ], factory);
  else root.foo = factory(root.bar, root.baz, root.bam);                         
}(this, function (bar, baz, bam) {                                               
  return {};                                                                      
}));
snippet umd "UMD Module Boilerplate"
`!p
deps = [] if t[2] == '' else t[2].split(', ')
`(function (root, factory) {
if (typeof exports === 'object') module.exports = factory(`!p
requires = map(lambda dep: 'require(\'' + dep + '\')', deps)
snip.rv = string.join(requires, ', ')
`);
else if (typeof define === 'function' && define.amd) define(`!p
if len(deps) is 0:
snip.rv = ''
else:
depNames = map(lambda dep: '\'' + dep + '\'', deps)
snip.rv = '[ ' + string.join(depNames, ', ') + ' ], '
`factory);
else root.${1:moduleName} = factory(`!p
rootDeps = map(lambda dep: 'root.' + dep, deps)
snip.rv = string.join(rootDeps, ', ')
`);
}(this, function (${2}) {
return {};
}));
endsnippet
snippet umdp "UMD Module Boilerplate - Pretty Version"
`!p
deps = [] if t[2] == '' else t[2].split(', ')
`(function (root, factory) {
// Node
if (typeof exports === 'object') {
module.exports = factory(`!p
if len(deps) is 0:
snip.rv = ''
else:
requires = map(lambda dep: 'require(\'' + dep + '\')', deps)
snip.rv = '\n ' + string.join(requires, '\n , ') + '\n '
`);
}
// AMD
else if (typeof define === 'function' && define.amd) {
define(`!p
if len(deps) is 0:
snip.rv = ''
else:
depNames = map(lambda dep: '\'' + dep + '\'', deps)
snip.rv = '[\n ' + string.join(depNames, '\n , ') + '\n ], '
`factory);
}
// Export to global
else {
root.${1:moduleName} = factory(`!p
if len(deps) is 0:
snip.rv = ''
else:
rootDeps = map(lambda dep: 'root.' + dep, deps)
snip.rv = '\n ' + string.join(rootDeps, '\n , ') + '\n '
`);
}
}(this, function (${2}) {
// Exported module API
return {};
}));
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment