Minor improvements to type checking

This commit is contained in:
Josh Johnson 2017-02-17 09:23:52 +00:00
parent bc36c3c5af
commit c63d025e96
3 changed files with 47 additions and 27 deletions

View file

@ -16,6 +16,7 @@ import {
isScrolledIntoView,
getAdjacentEl,
wrap,
getType,
isType,
isElement,
strToEl,
@ -709,34 +710,42 @@ class Choices {
/**
* Set value of input. If the input is a select box, a choice will be created and selected otherwise
* an item will created directly.
* @param {Array} args Array of value objects or value strings
* @param {Array/String} args Array of value objects or value strings/Single value string
* @return {Object} Class instance
* @public
*/
setValue(args) {
if (this.initialised === true) {
// Convert args to an itterable array
// Convert args to an iterable array
const values = [...args],
passedElementType = this.passedElement.type;
passedElementType = this.passedElement.type,
handleValue = (item) => {
const itemType = getType(item);
if (itemType === 'Object') {
if (!item.value) return;
// If we are dealing with a select input, we need to create an option first
// that is then selected. For text inputs we can just add items normally.
if (passedElementType !== 'text') {
this._addChoice(true, false, item.value, item.label, -1);
} else {
this._addItem(item.value, item.label, item.id);
}
} else if (itemType === 'String') {
if (passedElementType !== 'text') {
this._addChoice(true, false, item, item, -1);
} else {
this._addItem(item);
}
}
};
values.forEach((item) => {
if (isType('Object', item)) {
if (!item.value) return;
// If we are dealing with a select input, we need to create an option first
// that is then selected. For text inputs we can just add items normally.
if (passedElementType !== 'text') {
this._addChoice(true, false, item.value, item.label, -1);
} else {
this._addItem(item.value, item.label, item.id);
}
} else if (isType('String', item)) {
if (passedElementType !== 'text') {
this._addChoice(true, false, item, item, -1);
} else {
this._addItem(item);
}
}
});
if (values.length > 1) {
values.forEach((value) => {
handleValue(value);
});
} else {
handleValue(values[0]);
}
}
return this;
}
@ -2235,10 +2244,11 @@ class Choices {
} else if (this.isTextElement) {
// Add any preset values seperated by delimiter
this.presetItems.forEach((item) => {
if (isType('Object', item)) {
const itemType = getType(item);
if (itemType === 'Object') {
if (!item.value) return;
this._addItem(item.value, item.label, item.id);
} else if (isType('String', item)) {
} else if (itemType === 'String') {
this._addItem(item);
}
});

View file

@ -10,6 +10,16 @@ export const capitalise = function(str) {
});
};
/**
* Tests the type of an object
* @param {String} type Type to test object against
* @param {Object} obj Object to be tested
* @return {Boolean}
*/
export const getType = function(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
};
/**
* Tests the type of an object
* @param {String} type Type to test object against
@ -17,7 +27,7 @@ export const capitalise = function(str) {
* @return {Boolean}
*/
export const isType = function(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
var clas = getType(obj);
return obj !== undefined && obj !== null && clas === type;
};
@ -30,7 +40,7 @@ export const isNode = (o) => {
return (
typeof Node === "object" ? o instanceof Node :
o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string"
);
);
};
/**
@ -42,7 +52,7 @@ export const isElement = (o) => {
return (
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string"
);
);
};
/**

View file

@ -289,7 +289,7 @@
var textEmailFilter = new Choices('#choices-text-email-filter', {
editItems: true,
regexFilter: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
});
}).setValue(['joe@bloggs.com']);
var textDisabled = new Choices('#choices-text-disabled', {
addItems: false,