Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 63a124583cbd3943a38147d5737b9dbbd271d949 @rauchg rauchg committed Oct 18, 2012
Showing with 179 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +9 −0 Makefile
  3. +20 −0 Readme.md
  4. +14 −0 component.json
  5. +59 −0 index.js
  6. +16 −0 package.json
  7. +60 −0 test/clone.js
1 .gitignore
@@ -0,0 +1 @@
+node_modules
9 Makefile
@@ -0,0 +1,9 @@
+
+REPORTER = dot
+
+test:
+ @./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+ --bail
+
+.PHONY: test
20 Readme.md
@@ -0,0 +1,20 @@
+
+# clone
+
+ Object clone supporting `date`, `regexp`, `array` and `object` types.
+
+## Example
+
+```js
+var obj = clone({ a: 'b' })
+```
+
+## API
+
+### clone(obj)
+
+ Returns the cloned object.
+
+## License
+
+MIT
14 component.json
@@ -0,0 +1,14 @@
+{
+ "name": "clone",
+ "version": "0.1.0",
+ "dependencies": {
+ "component/type": "*"
+ },
+ "devDependencies": {
+ "visionmedia/mocha": "*",
+ "guille/expect.js": "*"
+ },
+ "scripts": [
+ "index.js"
+ ]
+}
59 index.js
@@ -0,0 +1,59 @@
+
+/**
+ * Module dependencies.
+ */
+
+var type;
+
+try {
+ type = require('type');
+} catch(e){
+ type = require('type-component');
+}
+
+/**
+ * Module exports.
+ */
+
+module.exports = clone;
+
+/**
+ * Clones objects.
+ *
+ * @param {Mixed} any object
+ * @api public
+ */
+
+function clone(obj){
+ switch (type(obj)) {
+ case 'object':
+ var copy = {};
+ for (var key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ copy[key] = clone(obj[key]);
+ }
+ }
+ return copy;
+
+ case 'array':
+ var copy = new Array(obj.length);
+ for (var i = 0, l = obj.length; i < l; i++) {
+ copy[i] = clone(obj[i]);
+ }
+ return copy;
+
+ case 'regexp':
+ // from millermedeiros/amd-utils - MIT
+ var flags = '';
+ flags += obj.multiline ? 'm' : '';
+ flags += obj.global ? 'g' : '';
+ flags += obj.ignoreCase ? 'i' : '';
+ return new RegExp(obj.source, flags);
+
+ case 'date':
+ return new Date(obj.getTime());
+
+ default: // string, number, boolean, …
+ return obj;
+ }
+}
16 package.json
@@ -0,0 +1,16 @@
+{
+ "name": "clone",
+ "version": "0.1.0",
+ "dependencies": {
+ "type-component": "*"
+ },
+ "devDependencies": {
+ "mocha": "*",
+ "expect.js": "*"
+ },
+ "component": {
+ "scripts": {
+ "clone/index.js": "index.js"
+ }
+ }
+}
60 test/clone.js
@@ -0,0 +1,60 @@
+
+/* test dependencies */
+
+var clone = require('..');
+var expect = require('expect.js');
+
+/* tests */
+
+describe('clone', function(){
+
+ it('date', function(){
+ var obj = new Date;
+ var cloned = clone(obj);
+ expect(cloned.getTime()).to.be(obj.getTime());
+ expect(cloned).not.to.be(obj);
+ });
+
+ it('regexp', function(){
+ var obj = /hello/i;
+ var cloned = clone(obj);
+ expect(cloned.toString()).to.be(obj.toString());
+ expect(cloned).not.to.be(obj);
+ });
+
+ it('array', function(){
+ var obj = [1, 2, 3, '4'];
+ var cloned = clone(obj);
+ expect(cloned).to.eql(obj);
+ expect(cloned).not.to.be(obj);
+ });
+
+ it('object', function(){
+ var obj = {
+ a: 1,
+ b: 2,
+ c: 3
+ };
+ var cloned = clone(obj);
+ expect(cloned).to.eql(obj);
+ expect(cloned).not.to.be(obj);
+ });
+
+ it('object combined', function(){
+ var date = new Date;
+ var obj = {
+ a: {
+ b: [1, 2, date, { hello: 'world' }]
+ }
+ };
+ var cloned = clone(obj);
+ expect(cloned).to.eql(obj);
+ expect(cloned.a).not.to.be(obj.a);
+ expect(cloned.a.b).not.to.be(obj.a.b);
+ expect(cloned.a.b[2]).not.to.be(obj.a.b[2]);
+ expect(cloned.a.b[2].getTime()).to.be(obj.a.b[2].getTime());
+ expect(cloned.a.b[3]).to.eql(obj.a.b[3]);
+ expect(cloned.a.b[3]).not.to.be(obj.a.b[3]);
+ });
+
+});

0 comments on commit 63a1245

Please sign in to comment.