Skip to content

Commit

Permalink
* isScalar() and hash() function added
Browse files Browse the repository at this point in the history
* some slight improvements
  • Loading branch information
Julien Polo committed Feb 22, 2011
1 parent ae69d69 commit 0bf8d8c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/altshift/core/dictionary.js
Expand Up @@ -173,15 +173,15 @@ var Dictionary = new klass.Class('Dictionary', {
* Traverse all Dictionary data calling iterator(value, key)
*
* @param {Function} iterator
* @param {*} thisp
* @return this
*/
forEach: function (iterator) {
forEach: function (iterator, thisp) {
if (! (iterator instanceof Function)) {
throw new TypeError('iterator should be a Function');
}
var key, thisp;
var key;

thisp = arguments[1];
for (key in this) {
if (this.hasOwnProperty(key)) {
iterator.call(thisp, this[key], key, this);
Expand Down
88 changes: 88 additions & 0 deletions lib/altshift/core/index.js
Expand Up @@ -88,6 +88,22 @@ function isObject(obj) {
);
}

/**
* Return true if `obj` is a scalar
*
* @param {*} obj
* @return {boolean}
*/
function isScalar(obj) {
var type = typeof obj;
return (type !== 'undefined' && (
obj === null ||
type === 'string' ||
type === 'number' ||
type === 'boolean'
));
}

/**
* Will destroy object
*
Expand All @@ -107,6 +123,7 @@ function destroy(object) {
}
}


/**
* Clone an object
*
Expand Down Expand Up @@ -162,6 +179,74 @@ function clone(obj, deep) {
return result; // Object
}

/**
* Iterate on object
*
* @param {*} object
* @param {Function} iterator
* @param {*} thisp
* @return undefined
*/
function forEach(object, iterator, thisp) {
if (object) {
if (object.forEach) {
object.forEach(iterator, thisp);
return;
}
//Default implementation
if (! (iterator instanceof Function)) {
throw new TypeError('iterator should be a Function');
}

var key, length;

if (isString(object)) {
length = object.length;
for (key = 0; key < length; key += 1) {
iterator.call(thisp, object[key], key, object);
}
return;
}

for (key in object) {
if (object.hasOwnProperty(key)) {
iterator.call(thisp, object[key], key, object);
}
}
}
}

/**
* Return a hash string for the object
*
* @param {*} object
* @return {String}
*/
function hash(object) {
if (object && isFunction(object.hash)) {
return object.hash();
}


if (isScalar(object)) {
return '' + object;
}

//Default implementation
var hashes = [];

forEach(object, function (value, key) {
var hashKey = String(key),
hashValue = hash(value);

hashes.push([hashKey, hashValue].sort().join(''));
});

return hashes.sort().join('');
}



/*******************************************************************************
* Shorcuts
******************************************************************************/
Expand Down Expand Up @@ -227,8 +312,11 @@ exports.interface = _interface;
exports.dict = _dict;
exports.destroy = destroy;

exports.forEach = forEach;
exports.hash = hash;
exports.mixin = mixin;
exports.isString = isString;
exports.isScalar = isScalar;
exports.isArray = isArray;
exports.isFunction = isFunction;
exports.isObject = isObject;
Expand Down
1 change: 1 addition & 0 deletions lib/altshift/http/index.js
Expand Up @@ -3,3 +3,4 @@ var core = require('../core');

core.mixin(exports, require('./client'));
core.mixin(exports, require('./server'));
core.mixin(exports, require('./jsgi'));
12 changes: 11 additions & 1 deletion lib/altshift/index.js
@@ -1,3 +1,13 @@
var core = require('./core');

core.mixin(exports, core);
core.mixin(exports, core);

['finder',
'fs',
'http',
'io',
'promise',
'uri'
].forEach(function (_module) {
exports[_module] = require('./' + _module);
});
2 changes: 1 addition & 1 deletion lib/altshift/uri.js
Expand Up @@ -227,6 +227,6 @@ var format = function (uriOrString) {
/**
* Exports
*/
Uri.export(module);
exports.Uri = Uri;
exports.parse = parse;
exports.format = format;
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "altshift",
"version": "0.3.3-2",
"version": "0.3.4",
"author": "Julien Polo <julien.polo@altshift.fr>",
"description": "Altshift open source framework",
"keywords": ["framework", "jsclass"],
Expand Down
43 changes: 43 additions & 0 deletions test/altshift/core-test.js
Expand Up @@ -103,6 +103,23 @@ var CoreTest = vows.describe('core module').addBatch({
assert.equal(topic(new StringSub()), true);
}
},
"isScalar()": {
topic: function () {
return core.isScalar;
},
'should return false for object, arrays, functions': function (topic) {
assert.equal(topic({}), false);
assert.equal(topic([]), false);
assert.equal(topic(undefined), false);
assert.equal(topic(function () {}), false);
},
'should return true for strings, number, booleans': function (topic) {
assert.equal(topic(true), true);
assert.equal(topic(null), true);
assert.equal(topic('my string'), true);
assert.equal(topic(1), true);
}
},
"isFunction()": {
topic: function () {
return core.isFunction;
Expand Down Expand Up @@ -172,6 +189,32 @@ var CoreTest = vows.describe('core module').addBatch({
assert.equal(topic.commonPrototypedProperty, 'commonPrototypedProperty1');
assert.isUndefined(topic.class2PrototypedProperty);
}
},
"hash()": {
topic: function () {
return core.hash;
},
'should convert to string scalars': function (topic) {
assert.equal(topic('str'), 'str');
assert.equal(topic(12), '12');
assert.equal(topic(true), 'true');
assert.equal(topic(null), 'null');
},
'should hash objects': function (topic) {
assert.equal(topic({
toto: 'str',
foo: 1
}), '1foostrtoto');
},
'should hash nested objects': function (topic) {
assert.equal(topic({
toto: 'str',
foo: 1,
bar: {
nested: true
}
}), '1foobarnestedtruestrtoto');
}
}
});

Expand Down

0 comments on commit 0bf8d8c

Please sign in to comment.