// Listen all fragments by "complete" eventcomplete.on("complete",function(fragment,word,line){returnthis.reply(["hello","world"]);});// Listen events by its order.complete.on("$1",function(word,line){returnthis.reply(["hello","world"]);});
Out of all the awesome module that James Halliday A.K.A substack has authored a module named rhyme a rhyming dictionary! This made it to the nmotw list ;)
The module makes use of CMU dictionary's data and picks up pronounce, syllables and plays around lazily to generate rhyming words for a given word.
vartemplate=Block(html);//html is a string, and block returns a new templating instance.template.render(locals);//locals is an object of "blocks" to replace in the template.
require('repl')._builtinLibs will give a list of all node buitins, if not in the list, it will look for the node_modules dir in the current working dir and then require the required there by making it less require-y!
Why?
As said by the author:
It's up to you. There's nothing wrong with the current system of multiple var declarations, and having too many isn't a node problem. Regardless, you may still consider it useful to namespace your dependencies under a name of your choosing, such as M or $, freeing up those "global" variables for use without conflicts.
GIF FTW!
Enjoy your less require-y week ;)
P.S: This module is just six days old! Will need to evolve on things like this. do contribute your ideas to make it more awesome!
Coding a "defensive" or a "contractual" API is a design decision, if you opt for the defensive way, here is a neat module called have that will help you to have your arguments, and validate it too!
Here is a simple example of two functions straight from the source, that does the same argument validations, the first one is without using have and the second one is with have:
12345678910111213141516171819
functionwithoutHave(id,arr,opts,callback){assert(typeofid==='string'||typeofid==='number','id argument not string or number');if(!(arrinstanceofArray)){arr=[arr];}for(vari=0;i<arr.length;i++){assert(typeofarr[i]==='string','arr member not a string');}if(typeofopts==='function'){callback=opts;opts={x:'some default value'};}assert(!opts||typeofopts==='object','options object not a hash');assert(typeofcallback==='function','callback missing or not a function');// logic...}
12345678910111213141516
functionwithHave(id,arr,opts,callback){have(arguments,{id:'str or num',arr:'str or str array',opts:'optional obj',callback:'func'});if(!(arrinstanceofArray)){arr=[arr];}if(typeofopts==='function'){callback=opts;opts={x:'some default value'};}// logic...}
How's have better than any other argument validator?
assert.js:92
throw new assert.AssertionError({ ^
AssertionError: inside function: test, one argument is not string
at funcName (/private/tmp/k.js:6:3) at ensure (/private/tmp/node_modules/have/have.js:109:5) at have (/private/tmp/node_modules/have/have.js:124:11) at have.one (/private/tmp/k.js:12:5) at test(/private/tmp/k.js:19:3) at Object.<anonymous> (/private/tmp/k.js:22:1) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12)
GIF FTW!
Thanks to Stdarg for suggesting this module and also a special thanks to chakrit the author of 'have' module
varConfigstore=require('configstore');varconf=newConfigstore('appConf');/* var conf = new ( require('configstore') )('appConf'); var conf = new ( require('configstore') )\ ('appConf', {'ans': 42}); i.e Configstore(id, defaults); id -> mandator defaults -> optional*/// Set some values.conf.set('life',42);conf.set('key',{'scrt':'42'});// Get some values.conf.get('life');// 42conf.get('key');// {'scrt': 42}// Delete a value.conf.del('life');conf.get('life');// undefined.// Get the count.conf.size()// 1// Reset the entier conf with new conf.conf.all={'ans':42};
Under the covers:
Config is stored as a YAML file in $XDG_CONFIG_HOME or ~/.config/configstore/your-config.yaml there is an issue for JSON support as well.
For the above example:
123456
$ cat ~/.config/configstore/appConf.yaml
# Would be something likekey:
scrt: 42
life: 42
There are many wonderful node modules that adhere to Unix philosophy:
"Write programs that do one thing and do it well."
Out of many such one such elegant module is chalk thanks to Sindre Sorhus for that.
Whenever one wants to color the terminal, as in styling the strings on the console the first module that would come to mind is colors.js but one of the major drawbacks with that being it extending String.portotye or be these problems.
Installing chalk is like any other module: npm install --save chalk
Apart from allow the normal styling syntax, it also provides multiple styles, nested styles and multiple arguments.