Esempio n. 1
0
function docrev(doc) {
    var doctype = type(doc);
    if (doctype === 'string' || doctype === 'number') return doc;
    if (doctype === 'object') {
        if (type(doc._rev)) return doc._rev;
        if (type(doc.rev)) return doc.rev;
    }
    return null;
}
Esempio n. 2
0
function docid(doc) {
    var doctype = type(doc);
    if (doctype === 'string' || doctype === 'number') return doc;
    if (doctype === 'object') {
        if (type(doc._id)) return doc._id;
        if (type(doc.id)) return doc.id;
    }
    return null;
}
Esempio n. 3
0
module.exports = function ( object, keys, keyType ) {

  keys = type( keys ) === "string" ? keys.split( "." ) : [];

  return hasKey( object, keys, keyType );

};
var dispatch = function(emitter, fn, err) {
  if(type(fn) !== 'function')
    return emitter.emit('error', err)

  fn(err)
  return true
}
Esempio n. 5
0
function hasKey( object, keys, keyType ) {

  object = type( object ) === "object" ? object : {}, keys = type( keys ) === "array" ? keys : [];
  keyType = type( keyType ) === "string" ? keyType : "";

  var key = keys.length > 0 ? keys.shift() : "",
    keyExists = has.call( object, key ) || object[ key ] !== void 0,
    keyValue = keyExists ? object[ key ] : undefined,
    keyTypeIsCorrect = type( keyValue ) === keyType;

  if ( keys.length > 0 && keyExists ) {
    return hasKey( object[ key ], keys, keyType );
  }

  return keys.length > 0 || keyType === "" ? keyExists : keyExists && keyTypeIsCorrect;

}
Esempio n. 6
0
  var stream = through.obj(function(chunk, enc, fn) {
    if (type(chunk) === 'string') {
      chunk = new Buffer(chunk)
    }

    parser.write(chunk)
    fn()
  })
Esempio n. 7
0
exports.set = function(obj, path, val){
  if (~path.indexOf('.')) {
    var par = parent(obj, path, true);
    var mainKey = path.split('.').pop();
    if (par && 'object' == type(par)) par[mainKey] = val;
  } else {
    obj[path] = val;
  }
};
Esempio n. 8
0
Relax.prototype.uuids = function(count, cb) {
    if (type(count) === 'function') cb = count, count = 1;
    var path = this.opts.server + '/_uuids';
    var req = request.get(path).query({count:count})
    if (!cb) return req;
    req.end(function(err, res){
        (res.ok) ? cb(null, JSON.parse(res.text)) : cb(err, null);
    });
};
Esempio n. 9
0
relation.prototype.one = function (from, opts, fn) {
  if(type(opts) !== 'object') fn = opts
  if(assertions(this.model, fn)(from)) return
  opts.limit = 1

  return cursor(this.get(from, opts)).all(function(err, tos){
    fn(err, tos ? tos.shift() : tos)
  })
}
Esempio n. 10
0
var check = function(x, y) {
  if (type(x) === 'string') {
    return y === x
  }

  if (x && type(x.exec) === 'function') {
    return x.exec(y)
  }

  if (type(x) === 'boolean') {
    return x
  }

  if (type(x) === 'function') {
    return x(y)
  }

  return false
}
Esempio n. 11
0
exports.get = function(obj, path){
  if (~path.indexOf('.')) {
    var par = parent(obj, path);
    var mainKey = path.split('.').pop();
    var t = type(par);
    if ('object' == t || 'array' == t) return par[mainKey];
  } else {
    return obj[path];
  }
};
Esempio n. 12
0
relation.prototype.each = function (from, opts, each, end) {
  if(type(opts) !== 'object') {
    end = each
    each = opts
  }

  if(assertions.fns(this.model)(each, end)) return
  if(assertions(this.model, end)(from)) return

  return cursor(this.get(from, opts)).each(each, end)
}
Esempio n. 13
0
function accepts (formats, reject) {
  if (type(formats) === 'string') formats = formats.split(/\s+/)
  return function middleware (req, res, next) {
    if (lookup(formats, req.accepted)) return next()
    if (reject) {
      log('Rejecting request with reject middleware')
      return reject(req, res, next)
    }
    log('Rejecting request')
    res.send(406)
  }
}
Esempio n. 14
0
  add: function (disposable) {

    if (disposable.dispose) {
      this._garbage.push(disposable);
    } else if (type(disposable) === "function") {
      this._garbage.push({
        dispose: disposable
      });
    }

    return this;
  },
Esempio n. 15
0
 _createSectionView: function (options) {
   var t;
   if ((t = type(options.type)) === "object") {
     return options.type;
   } else if (t === "function") {
     return new options.type(options);
   } else if (t === "string") {
     return this.view.application.createView(options.type, options);
   } else {
     throw new Error("cannot create section for type '" + t + "'");
   }
 }
Esempio n. 16
0
File: index.js Progetto: focampo/app
exports.merge = function merge (a, b){
  for (var key in b) {
    if (has.call(b, key) && b[key] != null) {
      if (!a) a = {};
      if ('object' === type(b[key])) {
        a[key] = exports.merge(a[key], b[key]);
      } else {
        a[key] = b[key];
      }
    }
  }
  return a;
};
Esempio n. 17
0
function parent(obj, key, init){
  if (~key.indexOf('.')) {
    var pieces = key.split('.');
    var ret = obj;

    for (var i = 0; i < pieces.length - 1; i++) {
      // if the key is a number string and parent is an array
      if (Number(pieces[i]) == pieces[i] && 'array' == type(ret)) {
        ret = ret[pieces[i]];
      } else if ('object' == type(ret)) {
        if (init && !ret.hasOwnProperty(pieces[i])) {
          ret[pieces[i]] = {};
        }
        if (ret) ret = ret[pieces[i]];
      }
    }

    return ret;
  } else {
    return obj;
  }
}
Esempio n. 18
0
Manager.prototype.process = function(obj){
  if ('object' != type(obj)) return;
  for (var i in obj) {
    if (obj.hasOwnProperty(i)) {
      var val = obj[i];
      if ('object' == type(val)) {
        if (val.$oid) {
          // $oid => string
          obj[i] = val.$oid;
        } else if (val.$date) {
          // $date => Date(ts)
          obj[i] = new Date(val.$date);
        } else {
          // recurse
          this.process(obj[i]);
        }
      } else if ('array' == type(val)) {
        for (var ii = 0; ii < val.length; ii++) {
          this.process(val[ii]);
        }
      }
    }
  }
};
Esempio n. 19
0
Relax.prototype.bulk = function(docs, cb) {
    var mess = 'docs isnt array';
    if ('array' != type(docs)) return (cb) ? cb(mess) : new Error(mess);
    var path = this.opts.dbpath + '/_bulk_docs';
    var req = request.post(path).send({docs: docs});

    if (!cb) return req;
    req.end(function(err, res) {
        if (err) {
            cb(err, null);
        } else {
            var json = JSON.parse(res.text.trim());
            cb(null, json);
        }
    });
};
Esempio n. 20
0
  _createChildView: function (options) {
    var t;

    if ((t = type(options.type)) === "object") {
      if (options.type.__isView) {
        return options.type;
      } else {
        return this.view.application.views.create("base", options.type);
      }
    } else if (t === "function") {
      return new options.type(options, this.view.application);
    } else if (t === "string") {
      return this.view.application.views.create(options.type, options);
    } else {
      throw new Error("cannot create child for type '" + t + "'");
    }
  }
Esempio n. 21
0
function cast(value) {
  var value_type = type(value)
  var ret = false
  if (value_type == 'date')
    return value

  if (value === '') return null;
  // support for timestamps
  var timestamp;
  if (value_type == 'number' || String(value) == (timestamp = Number(value)))
    ret = new Date(timestamp || value);

  // support for date strings
  else if (value.toString)
    ret = new Date(value.toString());

  if (ret && ret.toString() != 'Invalid Date')
    return ret;

  throw new TypeError('invalid date')
}
Esempio n. 22
0
function merge (a, b, opts){
  opts = (Boolean == typeof(opts))
    ? { inheritance: opts }
    : opts || { inheritance: false , shallow: false }


  for (var key in b) {
    var copy = !!opts.inheritance
     ? b[key] != null
     : (has.call(b, key)) && b[key] != null

    if (copy) {
      if (!a) a = {};
      if (!opts.shallow && 'object' === type(b[key])) {
        a[key] = merge(a[key], b[key], opts);
      } else {
        a[key] = b[key];
      }
    }
  }
  return a;
};
Esempio n. 23
0
exports.stringifyObject = function(open, sep, close, indent) {
  indent = indent || 0

  if (open === false) {
    open = ''
    sep = '\n'
    close = ''
  } else if (['null', 'undefined'].indexOf(type(open)) >= 0) {
    open = '{\n'
    sep = '\n,\n'
    close = '\n}\n'
  }

  var first = true

  return through.obj(function(chunk, enc, fn) {
    var json = JSON.stringify(chunk[0]) + ':' + JSON.stringify(chunk[1], null, indent)

    if (!first) {
      this.push(sep + json)
    } else {
      first = false
      this.push(open + json)
    }

    fn()
  }, function(fn) {
    if (first) {
      this.push(open)
    }

    first = false
    this.push(close)
    fn()
  })
}
 return Array.prototype.some.call(arguments, function (fn) {
   if(type(fn) !== 'function') return dispatch(emitter, fn, new Error('model expected'))
 })
 if(model.db && db_props.every(function (prop) {
   return type(model.db[prop]) === 'function'
 })) return
assert.fn = function (model, fn) {
  if(type(fn) === 'function')
    return

  return model.emit('error', new Error('expected callback'))
}
Esempio n. 27
0
File: utils.js Progetto: kordon/vnhr
 Array.prototype.slice.apply(arguments).forEach(function (argument) {
   if(type(argument) === 'string') args.servers = [argument]
   if(type(argument) === 'array') args.servers = argument
   if(type(argument) === 'object') args.options = merge(args.options, argument)
 })
Esempio n. 28
0
function check(value) {
  return type(value) === 'date'
}
Esempio n. 29
0
File: utils.js Progetto: kordon/vnhr
exports.array = function (length, n) {
  if(type(n) !== 'number') n = 0
  return Array.apply(null, Array(length)).map(Number.prototype.valueOf, n)
}
Esempio n. 30
0
relation.prototype.all = function (from, opts, fn) {
  if(type(opts) !== 'object') fn = opts
  if(assertions(this.model, fn)(from)) return

  return cursor(this.get(from, opts)).all(fn)
}