From d8e89900675163a0dc80979126a30f2648adb0e0 Mon Sep 17 00:00:00 2001 From: jkroso Date: Fri, 24 May 2013 15:05:25 +1200 Subject: [PATCH] add .swap() --- index.js | 13 +++++++++++++ test/classes.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/index.js b/index.js index e3a30d5..16845b7 100644 --- a/index.js +++ b/index.js @@ -162,3 +162,16 @@ ClassList.prototype.contains = function(name){ ? this.list.contains(name) : !! ~index(this.array(), name); }; + +/** + * Swap `a` for `b` + * + * @param {String} a + * @param {String} b + * @return {ClassList} + * @api public + */ + +ClassList.prototype.swap = function(a, b){ + return this.remove(a).add(b); +}; diff --git a/test/classes.js b/test/classes.js index c9c7135..4bc51c6 100644 --- a/test/classes.js +++ b/test/classes.js @@ -92,4 +92,19 @@ describe('classes(el)', function(){ assert(true === classes(el).has('there')); }) }) + + describe('.swap(old, new)', function(){ + it('should remove `old` and add `new`', function(){ + el.className = 'old'; + classes(el).swap('old', 'new'); + assert('new' === el.className); + }) + + it('should\'t care if `old` doesn\'t exist', function(){ + el.className = 'foo'; + classes(el).swap('old', 'new'); + assert(true === classes(el).has('foo')); + assert(false === classes(el).has('old')); + }) + }) })