Skip to content

Loading…

[ready] create missing $ prefixed aliases for pop, shift and unshift #884

Merged
merged 1 commit into from

2 participants

Showing with 39 additions and 3 deletions.
  1. +7 −3 lib/types/array.js
  2. +32 −0 test/types.array.test.js
View
10 lib/types/array.js
@@ -193,9 +193,10 @@ MongooseArray.prototype.$pushAll = function (value) {
* @api public
*/
+MongooseArray.prototype.pop =
MongooseArray.prototype.$pop = function () {
this._registerAtomic('$pop', 1);
- return this.pop();
+ return [].pop.call(this)
};
/**
@@ -208,9 +209,10 @@ MongooseArray.prototype.$pop = function () {
* @api public
*/
-MongooseArray.prototype.$shift = function () {
+MongooseArray.prototype.shift =
+MongooseArray.prototype.$shift = function $shift () {
this._registerAtomic('$pop', -1);
- return this.shift();
+ return [].shift.call(this)
};
/**
@@ -323,6 +325,7 @@ MongooseArray.prototype.splice = function () {
* @api public
*/
+MongooseArray.prototype.$unshift =
MongooseArray.prototype.unshift = function () {
var values = [].map.call(arguments, this._cast, this);
[].unshift.apply(this, values);
@@ -331,6 +334,7 @@ MongooseArray.prototype.unshift = function () {
};
/**
+MongooseArray.prototype.$shift =
* Adds values to the array if not already present.
* @api public
*/
View
32 test/types.array.test.js
@@ -63,6 +63,38 @@ module.exports = {
},
+ '$push is push': function() {
+ var db = start()
+ var User = db.model('User')
+ var tj = new User({ name: 'tj', pets: []})
+ tj.pets.push.should.equal(tj.pets.$push)
+ db.close()
+ },
+
+ '$pop is pop': function() {
+ var db = start()
+ var User = db.model('User')
+ var tj = new User({ name: 'tj', pets: []})
+ tj.pets.pop.should.equal(tj.pets.$pop)
+ db.close()
+ },
+
+ '$shift is shift': function() {
+ var db = start()
+ var User = db.model('User')
+ var tj = new User({ name: 'tj', pets: []})
+ tj.pets.shift.should.equal(tj.pets.$shift)
+ db.close()
+ },
+
+ '$unshift is unshift': function() {
+ var db = start()
+ var User = db.model('User')
+ var tj = new User({ name: 'tj', pets: []})
+ tj.pets.unshift.should.equal(tj.pets.$unshift)
+ db.close()
+ },
+
'test indexOf()': function(){
var db = start()
, User = db.model('User', 'users_' + random())
Something went wrong with that request. Please try again.