From 038f8d893a16142c51dbf7bf1338d50364cbc867 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Wed, 24 Sep 2014 17:09:41 +0800 Subject: [PATCH] Updates to JS, bluebird, and so on. --- bluebird.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ editorconfig.md | 37 ++++++++++++++++++++ inline-docs.md | 38 ++++++++++++++++++++ js-array.md | 1 + jshint.md | 2 ++ stylus.md | 54 ++++++++++++++++++++++++++--- 6 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 bluebird.md create mode 100644 editorconfig.md create mode 100644 inline-docs.md diff --git a/bluebird.md b/bluebird.md new file mode 100644 index 000000000..cc356e0a4 --- /dev/null +++ b/bluebird.md @@ -0,0 +1,92 @@ +--- +title: bluebird.js +layout: default +--- + +### Creating promises + +```js +var Promise = require("bluebird"); + +new Promise(function (ok, err) { + doStuff(function () { + if (success) + ok(); + else + err(); + }); +}) +``` + +### Consuming promises + +```js +promise + .then(okFn, errFn) + .spread(okFn, errFn) + .catch(errFn) + .catch(TypeError, errFn) + .finally(fn) +``` + +### Handling simultaneous promises + +Via Arrays + +```js +var promises = [ + promise1(), promise2(), ... +] + +Promise.all(promises) // succeeds when all succeed + .then(...) +Promise.any(promises) // succeeds if one succeeds first + .then(...) +``` + +or Objects + +```js +Promise.props({ + photos: get('photos'), + posts: get('posts') +}) +.then(function (res) { + res.photos + res.posts +}) +``` + +### Turn foreign promises into Bluebird promises + +```js +Promise.resolve($.get('http://google.com')) +.then(...) +``` + +### Consuming arrays + +```js +getFiles() + .map(function (e) { ... }) + .each(function (e) { ... }); +``` + +### Chain of promises + +```js +Promise.try(function () { + if (err) throw new Error("boo"); + return result; +}); + +### Working with node-style functions + +```js +var readFile = Promise.promisify(fs.readFile); +var fs = Promise.promisifyAll(require('fs')); +``` + +### References + + * https://github.com/petkaantonov/bluebird/blob/master/API.md diff --git a/editorconfig.md b/editorconfig.md new file mode 100644 index 000000000..829516da5 --- /dev/null +++ b/editorconfig.md @@ -0,0 +1,37 @@ +--- +title: editorconfig +layout: default +--- + + +```conf +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +# 4 space indentation +[*.py] +indent_style = space +indent_size = 4 + +# Tab indentation (no size specified) +[*.js] +indent_style = tab + +# Indentation override for all JS under lib directory +[lib/**.js] +indent_style = space +indent_size = 2 + +# Matches the exact files either package.json or .travis.yml +[{package.json,.travis.yml}] +indent_style = space +indent_size = 2 +``` + +See: http://EditorConfig.org + diff --git a/inline-docs.md b/inline-docs.md new file mode 100644 index 000000000..264e72eee --- /dev/null +++ b/inline-docs.md @@ -0,0 +1,38 @@ +--- +title: Inline documentation +layout: default +--- + +* Ruby: rdoc +* JavaScript: jsdoc + +### RDoc + + # Gets a circle's area + # + # @example + # + # area(3) + # #=> 28.27 + # + # @param [Number] r The radius of the ricle + # @return [true] If so + # + # == Definition lists + # + # list:: hi. + # +foo+:: parameterized + # + # == Definition lists + # [foo] also + # [bar] like this + +http://rdoc.rubyforge.org/RDoc/Markup.html + +### Jsdoc + + /** + * Ads numbers + * + * @this {Circle} + * @param {Number} r The radius diff --git a/js-array.md b/js-array.md index e55220ce0..9727784f8 100644 --- a/js-array.md +++ b/js-array.md @@ -12,6 +12,7 @@ layout: default ### Subsets + array.slice(0,1) //=> [a] array.slice(1) //=> [b,c,d,e] array.slice(1,2) //=> [b] diff --git a/jshint.md b/jshint.md index 3d5df5373..b13f5e78f 100644 --- a/jshint.md +++ b/jshint.md @@ -25,6 +25,8 @@ Enable these options to *not* throw errors in these conditions. /* jshint expr: true */ production && minify = true; + div.innerWidth; + expect(x).be.true; /* jshint laxcomma: true */ var one = 1 diff --git a/stylus.md b/stylus.md index 6e6fe6c51..06a47b6ca 100644 --- a/stylus.md +++ b/stylus.md @@ -56,10 +56,22 @@ Multiple args: ### Color operators - #888 + 50% // => #c3c3c3 - #888 - 50% // => #444 + #888 + 50% // => #c3c3c3 (darken) + #888 - 50% // => #444 (lighten) #f00 + 50deg // => #ffd500 (hue) +### Casting + + n = 5px + foo: (n)em + foo: (n * 5)% + +### Lookup + + light-blue = #3bd + name = 'blue' + lookup('light-' + name) + ### Built-in functions alpha(#fff) //=> 1 @@ -78,8 +90,6 @@ Multiple args: unquote(string) - s("rgba(0, 0, 0, %s)", 0.3) // Works like eval? - Add Property: gradient(color) @@ -88,3 +98,39 @@ Add Property: body background: gradient(red) + +### Conditional + + if color == blue + else if true and true + else if 'hey' is not 'bye' + + // Aliases: + == is + != is not + != isnt + +### Definition + + if ohnoes is defined + color: blue + +### False values + + 0 + null + false + '' + +### Type check + + if val is a 'string' + if val is a 'ident' + +### sprintf + + '-webkit-gradient(%s, %s, %s)' % (linear (0 0) (0 100%)) + // => -webkit-gradient(linear, 0 0, 0 100%) + + s("rgba(0, 0, 0, %s)", 0.3) +