Repository: gcanti/tcomb (js)

Issue:
t.update module: $apply doesn't play well with dates and regexps
The following test throws `undefined is not a function`:

```js
it('should $apply dates', function () {
  var instance = new Date(1973, 10, 30);
  var actual = update(instance, { $apply: function (date) { return date.getFullYear(); } });
  assert.equal(actual, 1973);
});
```

The following test throws `undefined == 'a'`

```js
it('should $apply regexps', function () {
  var instance = /a/;
  var actual = update(instance, { $apply: function (regexp) { return regexp.source; } });
  assert.equal(actual, 'a');
});
```

The culprit is `getShallowCopy`

```js
...
  if (isObject(x)) {
    return mixin({}, x);
  }
...
```

Pull request description:
fix #172

Patch:
diff --git a/lib/getShallowCopy.js b/lib/getShallowCopy.js
deleted file mode 100644
index e1e03a3..0000000
--- a/lib/getShallowCopy.js
+++ /dev/null
@@ -1,13 +0,0 @@
-var isArray = require('./isArray');
-var isObject = require('./isObject');
-var mixin = require('./mixin');
-
-module.exports = function getShallowCopy(x) {
-  if (isArray(x)) {
-    return x.concat();
-  }
-  if (isObject(x)) {
-    return mixin({}, x);
-  }
-  return x;
-};
\ No newline at end of file
diff --git a/lib/update.js b/lib/update.js
index e055bf3..399866a 100644
--- a/lib/update.js
+++ b/lib/update.js
@@ -4,9 +4,20 @@ var isFunction = require('./isFunction');
 var isArray = require('./isArray');
 var isNumber = require('./isNumber');
 var mixin = require('./mixin');
-var getShallowCopy = require('./getShallowCopy');
 
-// immutability helper
+function getShallowCopy(x) {
+  if (isArray(x)) {
+    return x.concat();
+  }
+  if (x instanceof Date || x instanceof RegExp) {
+    return x;
+  }
+  if (isObject(x)) {
+    return mixin({}, x);
+  }
+  return x;
+}
+
 function update(instance, spec) {
 
   if (process.env.NODE_ENV !== 'production') {


Test patch:
diff --git a/test/update.js b/test/update.js
index 7d32e11..c37dd9b 100644
--- a/test/update.js
+++ b/test/update.js
@@ -49,6 +49,18 @@ describe('t.update(instance, spec)', function () {
     assert.deepEqual(actual, [1, 3, 3]);
   });
 
+  it('should $apply dates', function () {
+    var instance = new Date(1973, 10, 30);
+    var actual = update(instance, { $apply: function (date) { return date.getFullYear(); } });
+    assert.equal(actual, 1973);
+  });
+
+  it('should $apply regexps', function () {
+    var instance = /a/;
+    var actual = update(instance, { $apply: function (regexp) { return regexp.source; } });
+    assert.equal(actual, 'a');
+  });
+
   it('should handle $unshift command', function () {
     var actual = update([1, 2, 3], {'$unshift': [4]});
     assert.deepEqual(actual, [4, 1, 2, 3]);
@@ -225,7 +237,7 @@ describe('t.update(instance, spec)', function () {
       var updated = update(instance, {$remove: ['a']});
       assert.deepEqual(updated, {b: 2});
     });
-  
+
     it('can $merge and $remove at once', function () {
       var updated = update(instance, { $merge: {a: [1, 2, 3] }, $remove: ['b'] });
       assert.deepEqual(updated, {a: [1, 2, 3]});