Skip to content

Commit

Permalink
Add UMD build. Closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilsson committed Jan 14, 2016
1 parent 277b966 commit abd3e01
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 193 deletions.
138 changes: 138 additions & 0 deletions dist/domtokenlist-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*! DOMTokenlist shim | Copyright 2016 Jonathan Wilsson and Bogdan Chadkin. */
;(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.DOMTokenList = factory();
}
}(this, function() {
'use strict';

var arr = [];

var inArray = function (array, value) {
var i;

if (arr.indexOf) {
return arr.indexOf.call(array, value);
}

for (i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}

return -1;
};

var validateToken = function (token) {
var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/;

if (token === '' || whitespace.test(token)) {
throw new Error('Token must not be empty or contain whitespace.');
}
};

var DOMTokenList = function (element, prop) {
var inst = this;
var i;
var values = [];

if (element && prop) {
inst.element = element;
inst.prop = prop;

if (element[prop]) {
values = element[prop].replace(/^\s+|\s+$/g, '').split(/\s+/);

for (i = 0; i < values.length; i++) {
inst[i] = values[i];
}
}
}

inst.length = values.length;
};

DOMTokenList.prototype = {
add: function () {
var inst = this;
var i;
var tokens = arguments;

for (i = 0; i < tokens.length; i++) {
validateToken(tokens[i]);

if (!inst.contains(tokens[i])) {
arr.push.call(inst, tokens[i]);
}
}

if (inst.element) {
inst.element[inst.prop] = inst;
}
},

contains: function (token) {
validateToken(token);

return inArray(this, token) !== -1;
},

item: function (index) {
return this[index] || null;
},

remove: function () {
var tokens = arguments;
var inst = this;
var key;
var i;

for (i = 0; i < tokens.length; i++) {
validateToken(tokens[i]);

key = inArray(inst, tokens[i]);

if (key !== -1) {
arr.splice.call(inst, key, 1);
}
}

if (inst.element) {
inst.element[inst.prop] = inst;
}
},

toggle: function (token, force) {
var inst = this;

if (inst.contains(token)) {
if (force) {
return true;
}

inst.remove(token);

return false;
} else {
if (force === false) {
return false;
}

inst.add(token);

return true;
}
},

toString: function () {
return arr.join.call(this, ' ');
}
};

return DOMTokenList;
}));
2 changes: 2 additions & 0 deletions dist/domtokenlist-umd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 90 additions & 91 deletions dist/domtokenlist.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! DOMTokenlist shim | Copyright 2015 Jonathan Wilsson and Bogdan Chadkin. */
/*! DOMTokenlist shim | Copyright 2016 Jonathan Wilsson and Bogdan Chadkin. */
;typeof window !== 'undefined' && (function (window) {
'use strict';

Expand Down Expand Up @@ -46,134 +46,133 @@
}
}(window));

;typeof window !== 'undefined' && (function (window) {
'use strict';
;typeof window !== 'undefined' && (function(window) {
'use strict';

var arr = [];
var arr = [];

var inArray = function (array, value) {
var i;
var inArray = function (array, value) {
var i;

if (arr.indexOf) {
return arr.indexOf.call(array, value);
}
if (arr.indexOf) {
return arr.indexOf.call(array, value);
}

for (i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
for (i = 0; i < array.length; i++) {
if (array[i] === value) {
return i;
}
}

return -1;
};
return -1;
};

var validateToken = function (token) {
var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/;
var validateToken = function (token) {
var whitespace = /[\u0009\u000A\u000C\u000D\u0020]/;

if (token === '' || whitespace.test(token)) {
throw new Error('Token must not be empty or contain whitespace.');
}
};
if (token === '' || whitespace.test(token)) {
throw new Error('Token must not be empty or contain whitespace.');
}
};

var DOMTokenList = function (element, prop) {
var inst = this;
var i;
var values = [];
var DOMTokenList = function (element, prop) {
var inst = this;
var i;
var values = [];

if (element && prop) {
inst.element = element;
inst.prop = prop;
if (element && prop) {
inst.element = element;
inst.prop = prop;

if (element[prop]) {
values = element[prop].replace(/^\s+|\s+$/g, '').split(/\s+/);
if (element[prop]) {
values = element[prop].replace(/^\s+|\s+$/g, '').split(/\s+/);

for (i = 0; i < values.length; i++) {
inst[i] = values[i];
}
for (i = 0; i < values.length; i++) {
inst[i] = values[i];
}
}
}

inst.length = values.length;
};
inst.length = values.length;
};

DOMTokenList.prototype = {
add: function () {
var inst = this;
var i;
var tokens = arguments;
DOMTokenList.prototype = {
add: function () {
var inst = this;
var i;
var tokens = arguments;

for (i = 0; i < tokens.length; i++) {
validateToken(tokens[i]);
for (i = 0; i < tokens.length; i++) {
validateToken(tokens[i]);

if (!inst.contains(tokens[i])) {
arr.push.call(inst, tokens[i]);
}
if (!inst.contains(tokens[i])) {
arr.push.call(inst, tokens[i]);
}
}

if (inst.element) {
inst.element[inst.prop] = inst;
}
},
if (inst.element) {
inst.element[inst.prop] = inst;
}
},

contains: function (token) {
validateToken(token);
contains: function (token) {
validateToken(token);

return inArray(this, token) !== -1;
},
return inArray(this, token) !== -1;
},

item: function (index) {
return this[index] || null;
},
item: function (index) {
return this[index] || null;
},

remove: function () {
var tokens = arguments;
var inst = this;
var key;
var i;
remove: function () {
var tokens = arguments;
var inst = this;
var key;
var i;

for (i = 0; i < tokens.length; i++) {
validateToken(tokens[i]);
for (i = 0; i < tokens.length; i++) {
validateToken(tokens[i]);

key = inArray(inst, tokens[i]);
key = inArray(inst, tokens[i]);

if (key !== -1) {
arr.splice.call(inst, key, 1);
}
if (key !== -1) {
arr.splice.call(inst, key, 1);
}
}

if (inst.element) {
inst.element[inst.prop] = inst;
}
},
if (inst.element) {
inst.element[inst.prop] = inst;
}
},

toggle: function (token, force) {
var inst = this;
toggle: function (token, force) {
var inst = this;

if (inst.contains(token)) {
if (force) {
return true;
}
if (inst.contains(token)) {
if (force) {
return true;
}

inst.remove(token);
inst.remove(token);

return false;
} else {
if (force === false) {
return false;
} else {
if (force === false) {
return false;
}

inst.add(token);

return true;
}
},

toString: function () {
return arr.join.call(this, ' ');
inst.add(token);

return true;
}
};
},

window.DOMTokenList = DOMTokenList;
toString: function () {
return arr.join.call(this, ' ');
}
};
window.DOMTokenList = DOMTokenList;
}(window));

;typeof window !== 'undefined' && (function () {
Expand Down
2 changes: 1 addition & 1 deletion dist/domtokenlist.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit abd3e01

Please sign in to comment.