Update filterMode option. Add tests for filterMode.

pull/149/head v4.2.0
Glen Cheney 7 years ago
parent 78f87fc6e1
commit 4f95fba70b

@ -1,6 +1,6 @@
<h2>Changes<a href="#changelog"></a></h2> <h2>Changes<a href="#changelog"></a></h2>
<ul> <ul>
<li><code>v4.2.0</code> 5/9/17 - Replace <code>webpack</code> build with <code>rollup</code>. Replace <code>jshint</code> and <code>jscs</code> with <code>eslint</code>.</li> <li><code>v4.2.0</code> 5/10/17 - Replace <code>webpack</code> build with <code>rollup</code>. Replace <code>jshint</code> and <code>jscs</code> with <code>eslint</code>. Add <code>filterMode</code> option.</li>
<li><code>v4.1.1</code> 3/21/17 - the <code>before</code> styles for a <code>ShuffleItem</code> were not applied if the item didn&rsquo;t move.</li> <li><code>v4.1.1</code> 3/21/17 - the <code>before</code> styles for a <code>ShuffleItem</code> were not applied if the item didn&rsquo;t move.</li>
<li><code>v4.1.0</code> 1/30/17 - Use webpack-2 to bundle Shuffle.</li> <li><code>v4.1.0</code> 1/30/17 - Use webpack-2 to bundle Shuffle.</li>
<li><code>v4.0.2</code> 9/15/16 - Update <code>custom-event-polyfill</code> dependency.</li> <li><code>v4.0.2</code> 9/15/16 - Update <code>custom-event-polyfill</code> dependency.</li>

@ -20,6 +20,7 @@ Shuffle.options = {
staggerAmount: 15, // Transition delay offset for each item in milliseconds. staggerAmount: 15, // Transition delay offset for each item in milliseconds.
staggerAmountMax: 250, // Maximum stagger delay in milliseconds. staggerAmountMax: 250, // Maximum stagger delay in milliseconds.
useTransforms: true, // Whether to use transforms or absolute positioning. useTransforms: true, // Whether to use transforms or absolute positioning.
filterMode: Shuffle.FilterMode.ANY, // When using an array with filter(), the element passes the test if any of its groups are in the array. With "all", the element only passes if all groups are in the array.
};</code></pre> };</code></pre>
</div> </div>

35
dist/shuffle.js vendored

@ -718,13 +718,7 @@ function toArray$$1(arrayLike) {
} }
function arrayIncludes(array, obj) { function arrayIncludes(array, obj) {
if (arguments.length === 2) { return array.indexOf(obj) > -1;
return arrayIncludes(array)(obj);
}
return function (obj) {
return array.indexOf(obj) > -1;
};
} }
// Used for unique instance variables // Used for unique instance variables
@ -958,18 +952,21 @@ var Shuffle = function () {
value: function _doesPassFilter(category, element) { value: function _doesPassFilter(category, element) {
if (typeof category === 'function') { if (typeof category === 'function') {
return category.call(element, element, this); return category.call(element, element, this);
// Check each element's data-groups attribute against the given category.
} }
// Check each element's data-groups attribute against the given category.
var attr = element.getAttribute('data-' + Shuffle.FILTER_ATTRIBUTE_KEY); var attr = element.getAttribute('data-' + Shuffle.FILTER_ATTRIBUTE_KEY);
var keys = this.options.delimeter ? attr.split(this.options.delimeter) : JSON.parse(attr); var keys = this.options.delimeter ? attr.split(this.options.delimeter) : JSON.parse(attr);
function testCategory(category) {
return arrayIncludes(keys, category);
}
if (Array.isArray(category)) { if (Array.isArray(category)) {
if (this.options.filterMode != Shuffle.filterMode.EXCLUSIVE) { if (this.options.filterMode === Shuffle.FilterMode.ANY) {
return category.every(arrayIncludes(keys)); return category.some(testCategory);
} else {
return category.some(arrayIncludes(keys));
} }
return category.every(testCategory);
} }
return arrayIncludes(keys, category); return arrayIncludes(keys, category);
@ -1925,9 +1922,9 @@ Shuffle.Classes = Classes;
/** /**
* @enum {string} * @enum {string}
*/ */
Shuffle.filterMode = { Shuffle.FilterMode = {
EXCLUSIVE: 'exclusive', ANY: 'any',
ADDITIVE: 'additive' ALL: 'all'
}; };
// Overrideable options // Overrideable options
@ -1988,8 +1985,10 @@ Shuffle.options = {
// Whether to use transforms or absolute positioning. // Whether to use transforms or absolute positioning.
useTransforms: true, useTransforms: true,
// Filters elements with "some" when 'exclusive' and with every on 'additive' // Affects using an array with filter. e.g. `filter(['one', 'two'])`. With "any",
filterMode: Shuffle.filterMode.EXCLUSIVE // the element passes the test if any of its groups are in the array. With "all",
// the element only passes if all groups are in the array.
filterMode: Shuffle.FilterMode.ANY
}; };
// Expose for testing. Hack at your own risk. // Expose for testing. Hack at your own risk.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -18,11 +18,7 @@ function toArray(arrayLike) {
} }
function arrayIncludes(array, obj) { function arrayIncludes(array, obj) {
if (arguments.length === 2) { return array.indexOf(obj) > -1;
return arrayIncludes(array)(obj);
}
return obj => array.indexOf(obj) > -1;
} }
// Used for unique instance variables // Used for unique instance variables
@ -230,19 +226,23 @@ class Shuffle {
_doesPassFilter(category, element) { _doesPassFilter(category, element) {
if (typeof category === 'function') { if (typeof category === 'function') {
return category.call(element, element, this); return category.call(element, element, this);
}
// Check each element's data-groups attribute against the given category. // Check each element's data-groups attribute against the given category.
}
const attr = element.getAttribute('data-' + Shuffle.FILTER_ATTRIBUTE_KEY); const attr = element.getAttribute('data-' + Shuffle.FILTER_ATTRIBUTE_KEY);
const keys = this.options.delimeter ? const keys = this.options.delimeter ?
attr.split(this.options.delimeter) : attr.split(this.options.delimeter) :
JSON.parse(attr); JSON.parse(attr);
function testCategory(category) {
return arrayIncludes(keys, category);
}
if (Array.isArray(category)) { if (Array.isArray(category)) {
if (this.options.filterMode !== Shuffle.filterMode.EXCLUSIVE) { if (this.options.filterMode === Shuffle.FilterMode.ANY) {
return category.every(arrayIncludes(keys)); return category.some(testCategory);
} }
return category.some(arrayIncludes(keys)); return category.every(testCategory);
} }
return arrayIncludes(keys, category); return arrayIncludes(keys, category);
@ -1035,9 +1035,9 @@ Shuffle.Classes = Classes;
/** /**
* @enum {string} * @enum {string}
*/ */
Shuffle.filterMode = { Shuffle.FilterMode = {
EXCLUSIVE: 'exclusive', ANY: 'any',
ADDITIVE: 'additive', ALL: 'all',
}; };
// Overrideable options // Overrideable options
@ -1098,8 +1098,10 @@ Shuffle.options = {
// Whether to use transforms or absolute positioning. // Whether to use transforms or absolute positioning.
useTransforms: true, useTransforms: true,
// Filters elements with "some" when 'exclusive' and with every on 'additive' // Affects using an array with filter. e.g. `filter(['one', 'two'])`. With "any",
filterMode: Shuffle.filterMode.EXCLUSIVE, // the element passes the test if any of its groups are in the array. With "all",
// the element only passes if all groups are in the array.
filterMode: Shuffle.FilterMode.ANY,
}; };
// Expose for testing. Hack at your own risk. // Expose for testing. Hack at your own risk.

@ -346,6 +346,17 @@ describe('shuffle', function () {
expect(instance._doesPassFilter(function (element) { expect(instance._doesPassFilter(function (element) {
return element.getAttribute('data-age') === '22'; return element.getAttribute('data-age') === '22';
}, first)).to.equal(false); }, first)).to.equal(false);
// Arrays.
expect(instance._doesPassFilter(['design'], first)).to.be.true;
expect(instance._doesPassFilter(['red'], first)).to.be.true;
expect(instance._doesPassFilter(['design', 'black'], first)).to.be.true;
// Change filter mode.
instance.options.filterMode = Shuffle.FilterMode.ALL;
expect(instance._doesPassFilter(['design'], first)).to.be.true;
expect(instance._doesPassFilter(['design', 'red'], first)).to.be.true;
expect(instance._doesPassFilter(['design', 'black'], first)).to.be.false;
}); });
it('will maintain the last sort object', function () { it('will maintain the last sort object', function () {

Loading…
Cancel
Save