-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 99e0529
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*sublime-* | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Installation: | ||
|
||
```npm install git+https://island-ebs:i51andghebs@github.com/The-Island/island-util.git#develop``` (read-only) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* index.js: Island utils. | ||
* | ||
*/ | ||
|
||
// Module Dependencies | ||
var crypto = require('crypto'); | ||
var util = require('util'); | ||
var _ = require('underscore'); | ||
_.mixin(require('underscore.string')); | ||
|
||
/* | ||
* Prepare obj for client. | ||
* - replace ObjectsIDs with strings. | ||
*/ | ||
exports.client = function (obj) { | ||
obj = _.clone(obj); | ||
if (obj._id) { | ||
obj.id = obj._id.toString(); | ||
delete obj._id; | ||
} | ||
_.each(obj, function (att, n) { | ||
if (_.isObject(att) && att._id) { | ||
att.id = att._id.toString(); | ||
delete att._id; | ||
exports.client(att); | ||
} else if (_.isObject(att) || _.isArray(att)) { | ||
exports.client(att); | ||
} | ||
}); | ||
return obj; | ||
}; | ||
|
||
/* | ||
* Creates a string identifier. | ||
* @length Number | ||
*/ | ||
exports.key = function (length) { | ||
length = length || 8; | ||
var key = ''; | ||
var possible = 'abcdefghijklmnopqrstuvwxyz0123456789'; | ||
for (var i = 0; i < length; ++i) { | ||
key += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return key; | ||
}; | ||
|
||
/* | ||
* Returns true if valid date | ||
* http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript | ||
*/ | ||
exports.isValidDate = function(d) { | ||
if (Object.prototype.toString.call(d) === "[object Date]") { | ||
if (!(isNaN(d.getTime()))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/* | ||
* Returns the first property of object that does not match given type. | ||
*/ | ||
exports.validateObjectTypes = function (types, props) { | ||
var list = _.map(types, function (v, k) { | ||
return {k: k, t: v}; | ||
}); | ||
var invalid = _.find(list, function (p) { | ||
return typeof props[p.k] !== p.t; | ||
}); | ||
if (invalid) { | ||
invalid.msg = invalid.k + ' must be a ' + invalid.t; | ||
} | ||
return invalid; | ||
} | ||
|
||
/* | ||
* Make salt for a password. | ||
*/ | ||
exports.salt = function () { | ||
return Math.round((new Date().valueOf() * Math.random())) + ''; | ||
}; | ||
|
||
/* | ||
* Encrypt string. | ||
*/ | ||
exports.encrypt = function (str, salt) { | ||
return crypto.createHmac('sha1', salt).update(str).digest('hex'); | ||
}; | ||
|
||
/* | ||
* Hash string. | ||
*/ | ||
exports.hash = function (str) { | ||
return crypto.createHash('md5').update(str).digest('hex'); | ||
}; | ||
|
||
/* | ||
* Create a 32-bit identifier. | ||
*/ | ||
exports.createId_32 = function () { | ||
return parseInt(Math.random() * 0x7fffffff); | ||
}; | ||
|
||
/* | ||
* Remove ''s from an object. | ||
*/ | ||
exports.removeEmptyStrings = function (obj) { | ||
_.each(obj, function (v, k) { | ||
if (_.isString(v) && v.trim() === '') { | ||
delete obj[k]; | ||
} | ||
}); | ||
}; | ||
|
||
/* | ||
* Convert tag string to array. | ||
*/ | ||
exports.tagify = function (str, delim) { | ||
var splitter = delim ? '[' + delim + ']': '[\\W,_]'; | ||
return !str ? []: _.chain(str.split(new RegExp(splitter))) | ||
.reject(function (t) { return t === ''; }) | ||
.map(function (t) { return t.trim(); }).uniq().value(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"private": true, | ||
"author": "The Island (git://github.com/The-Island)", | ||
"name": "island-util", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/The-Island/island-util.git" | ||
}, | ||
"dependencies": { | ||
"underscore": "^1.4.4", | ||
"underscore.string": "~2.3.1" | ||
} | ||
} | ||
|