Permalink
Browse files

Move 'typeOf' function to util module.

  • Loading branch information...
1 parent fdd02da commit 47bd2a5f3621125f2a9e7e75681a95998cdd156d @Kami Kami committed May 7, 2013
Showing with 30 additions and 28 deletions.
  1. +25 −0 lib/util.js
  2. +5 −28 lib/valve.js
View
25 lib/util.js
@@ -42,5 +42,30 @@ function merge(a, b) {
}
+/**
+ * A "better" typeof-like function that can distinguish between array and null
+ * objects. NOTE: This is a function, not an operator like "typeof".
+ *
+ * @private
+ * @param {value} value an object.
+ * @return {String} 'array' or 'null'.
+ */
+function typeOf(value) {
+ var t = typeof(value);
+ if (t === 'object') {
+ if (value) {
+ if (value instanceof Array) {
+ t = 'array';
+ }
+ }
+ else {
+ t = 'null';
+ }
+ }
+ return t;
+}
+
+
exports.trim = trim;
exports.merge = merge;
+exports.typeOf = typeOf;
View
33 lib/valve.js
@@ -60,6 +60,7 @@ var async = require('async'),
sanitize = require('validator').sanitize,
check = require('validator').check,
validators = require('./validators'),
+ utils = require('./util'),
net = require('net'),
ipv6 = require('ipv6').v6,
ipv4 = require('ipv6').v4,
@@ -140,30 +141,6 @@ function chainHelp(chain) {
/**
- * A "better" typeof-like function that can distinguish between array and null
- * objects. NOTE: This is a function, not an operator like "typeof".
- *
- * @private
- * @param {value} value an object.
- * @return {String} 'array' or 'null'.
- */
-function typeOf(value) {
- var t = typeof(value);
- if (t === 'object') {
- if (value) {
- if (value instanceof Array) {
- t = 'array';
- }
- }
- else {
- t = 'null';
- }
- }
- return t;
-}
-
-
-/**
* Normalize an IP address. Expands "::" notation in IPv6 addresses and
* zero-prefixes each component number (see ipv6.canonical_form()).
*
@@ -1173,7 +1150,7 @@ Chain.prototype.notIn = function(values, caseSensitive) {
this._pushValidator({
name: 'notIn',
func: function(value, baton, callback) {
- var type = typeOf(value), keys, key, i;
+ var type = utils.typeOf(value), keys, key, i;
if (type === 'string' || type === 'number') {
keys = [value];
@@ -1636,7 +1613,7 @@ Chain.prototype.isString = function() {
this._pushValidator({
name: 'isString',
func: function(value, baton, callback) {
- if (typeOf(value) !== 'string') {
+ if (utils.typeOf(value) !== 'string') {
callback('Not a string');
} else {
callback(null, value);
@@ -1820,7 +1797,7 @@ Chain.prototype.isArray = function(chain) {
this._pushValidator({
name: 'isArray',
func: function(value, baton, callback) {
- if (typeOf(value) !== 'array') {
+ if (utils.typeOf(value) !== 'array') {
callback('Not an array');
return;
}
@@ -1889,7 +1866,7 @@ Chain.prototype.isHash = function(keyChain, valueChain) {
var key,
kvpairs = [];
- if (typeOf(value) !== 'object') {
+ if (utils.typeOf(value) !== 'object') {
callback('Not a hash');
return;
}

0 comments on commit 47bd2a5

Please sign in to comment.