From da1d28e02e1761d89f1964ceac2a3c5d525340fd Mon Sep 17 00:00:00 2001 From: harttle Date: Tue, 25 Oct 2016 18:12:24 +0800 Subject: [PATCH] feature: unshift and shift for scope, working on #15 --- src/scope.js | 8 +++++++- test/scope.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/scope.js b/src/scope.js index 9a98fdef3..a7fd15231 100644 --- a/src/scope.js +++ b/src/scope.js @@ -41,7 +41,13 @@ var Scope = { pop: function() { return this.scopes.pop(); }, - + unshift: function(ctx) { + if (!ctx) throw new Error('trying to push $(ctx) into scopes'); + return this.scopes.unshift(ctx); + }, + shift: function() { + return this.scopes.shift(); + }, setPropertyByPath: function(obj, path, val) { if (_.isString(path)) { var paths = path.replace(/\[/g, '.').replace(/\]/g, '').split('.'); diff --git a/test/scope.js b/test/scope.js index 3a8332eca..7591438ac 100644 --- a/test/scope.js +++ b/test/scope.js @@ -113,5 +113,17 @@ describe('scope', function() { scope.pop(); expect(scope.get('foo')).to.equal('zoo'); }); + it('should unshift scope', function() { + scope.unshift({foo: 'blue', foo1: 'foo1'}) + scope.get('foo').should.equal('zoo'); + scope.get('foo1').should.equal('foo1'); + }); + + it('should shift scope', function() { + scope.unshift({foo: 'blue', foo1: 'foo1'}); + scope.shift(); + expect(scope.get('foo')).to.equal('zoo'); + expect(scope.get('foo1')).to.equal(undefined); + }); }); });