Browse Source

comments & formatting

Evan You 12 years ago
parent
commit
295b6d4572
6 changed files with 54 additions and 19 deletions
  1. 3 2
      src/directive.js
  2. 1 1
      src/directives/index.js
  3. 1 1
      src/directives/repeat.js
  4. 13 3
      src/exp-parser.js
  5. 26 11
      src/filters.js
  6. 10 1
      src/observer.js

+ 3 - 2
src/directive.js

@@ -1,9 +1,10 @@
 var config     = require('./config'),
     utils      = require('./utils'),
     directives = require('./directives'),
-    filters    = require('./filters')
+    filters    = require('./filters'),
 
-var KEY_RE          = /^[^\|]+/,
+    // Regexes!
+    KEY_RE          = /^[^\|]+/,
     ARG_RE          = /([^:]+):(.+)$/,
     FILTERS_RE      = /\|[^\|]+/g,
     FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,

+ 1 - 1
src/directives/index.js

@@ -35,7 +35,7 @@ module.exports = {
         this.el.style.visibility = value ? '' : 'hidden'
     },
 
-    class: function (value) {
+    'class': function (value) {
         if (this.arg) {
             this.el.classList[value ? 'add' : 'remove'](this.arg)
         } else {

+ 1 - 1
src/directives/repeat.js

@@ -178,7 +178,7 @@ module.exports = {
     },
 
     /**
-     *  Detach/ the container from the DOM before mutation
+     *  Detach/retach the container from the DOM before mutation
      *  so that batch DOM updates are done in-memory and faster
      */
     detach: function () {

+ 13 - 3
src/exp-parser.js

@@ -1,4 +1,5 @@
-// Variable extraction scooped from https://github.com/RubyLouvre/avalon 
+// Variable extraction scooped from https://github.com/RubyLouvre/avalon
+
 var KEYWORDS =
         // keywords
         'break,case,catch,continue,debugger,default,delete,do,else,false'
@@ -18,6 +19,9 @@ var KEYWORDS =
     NUMBER_RE   = /\b\d[^,]*/g,
     BOUNDARY_RE = /^,+|,+$/g
 
+/**
+ *  Strip top level variable names from a snippet of JS expression
+ */
 function getVariables (code) {
     code = code
         .replace(REMOVE_RE, '')
@@ -30,6 +34,11 @@ function getVariables (code) {
         : []
 }
 
+/**
+ *  Based on top level variables, extract full keypaths accessed.
+ *  We need full paths because we need to define them in the compiler's
+ *  bindings, so that they emit 'get' events during dependency tracking.
+ */
 function getPaths (code, vars) {
     var pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g')
     return code.match(pathRE)
@@ -38,8 +47,9 @@ function getPaths (code, vars) {
 module.exports = {
 
     /**
-     *  Parse and create an anonymous computed property getter function
-     *  from an arbitrary expression.
+     *  Parse and return an anonymous computed property getter function
+     *  from an arbitrary expression, together with a list of paths to be
+     *  created as bindings.
      */
     parse: function (exp) {
         // extract variable names

+ 26 - 11
src/filters.js

@@ -11,24 +11,46 @@ var keyCodes = {
 
 module.exports = {
 
+    /**
+     *  'abc' => 'Abc'
+     */
     capitalize: function (value) {
         if (!value && value !== 0) return ''
         value = value.toString()
         return value.charAt(0).toUpperCase() + value.slice(1)
     },
 
+    /**
+     *  'abc' => 'ABC'
+     */
     uppercase: function (value) {
         return (value || value === 0)
             ? value.toString().toUpperCase()
             : ''
     },
 
+    /**
+     *  'AbC' => 'abc'
+     */
     lowercase: function (value) {
         return (value || value === 0)
             ? value.toString().toLowerCase()
             : ''
     },
 
+    /**
+     *  12345 => $12,345.00
+     */
+    currency: function (value, args) {
+        if (!value && value !== 0) return ''
+        var sign = (args && args[0]) || '$',
+            s = Math.floor(value).toString(),
+            i = s.length % 3,
+            h = i > 0 ? (s.slice(0, i) + (s.length > 3 ? ',' : '')) : '',
+            f = '.' + value.toFixed(2).slice(-2)
+        return sign + h + s.slice(i).replace(/(\d{3})(?=\d)/g, '$1,') + f
+    },
+
     /**
      *  args: an array of strings corresponding to
      *  the single, double, triple ... forms of the word to
@@ -44,16 +66,10 @@ module.exports = {
             : (args[value - 1] || args[0] + 's')
     },
 
-    currency: function (value, args) {
-        if (!value && value !== 0) return ''
-        var sign = (args && args[0]) || '$',
-            s = Math.floor(value).toString(),
-            i = s.length % 3,
-            h = i > 0 ? (s.slice(0, i) + (s.length > 3 ? ',' : '')) : '',
-            f = '.' + value.toFixed(2).slice(-2)
-        return sign + h + s.slice(i).replace(/(\d{3})(?=\d)/g, '$1,') + f
-    },
-
+    /**
+     *  A special filter that takes a handler function,
+     *  wraps it so it only gets triggered on specific keypresses.
+     */
     key: function (handler, args) {
         if (!handler) return
         var code = keyCodes[args[0]]
@@ -66,5 +82,4 @@ module.exports = {
             }
         }
     }
-
 }

+ 10 - 1
src/observer.js

@@ -2,11 +2,20 @@
 
 var Emitter  = require('./emitter'),
     utils    = require('./utils'),
+
+    // cache methods
     typeOf   = utils.typeOf,
     def      = utils.defProtected,
     slice    = Array.prototype.slice,
+
+    // Array mutation methods to wrap
     methods  = ['push','pop','shift','unshift','splice','sort','reverse'],
-    hasProto = ({}).__proto__ // fix for IE9
+
+    // fix for IE + __proto__ problem
+    // define methods as inenumerable if __proto__ is present,
+    // otherwise enumerable so we can loop through and manually
+    // attach to array instances
+    hasProto = ({}).__proto__
 
 // The proxy prototype to replace the __proto__ of
 // an observed array