function searchEmployees(value) {
if (typeof value === 'string') {
if (value.trim().length > 1) {
return EmployeeDB.searchByName(value);
} else if (value.trim().length === 1) {
return EmployeeDB.searchByMiddleInitial(value);
} else {
return { error: 'Invalid search value supplied' };
}
} else if (typeof value === 'object' && value !== null) {
if (Object.keys(value).length === 1) {
return EmployeeDB.searchByFieldValuePair(value);
} else if (Object.keys(value).length > 1) {
return { error: 'Search by multiple fields not supported' };
} else {
return { error: 'Invalid search value supplied' };
}
} else if (typeof value === 'number') {
if (!isNaN(value) && isFinite(value) && value > 0 && value % 1 === 0) {
return EmployeeDB.searchByEmployeeNumber(value);
} else {
return { error: 'Invalid employee number supplied' };
}
} else if (typeof value === 'undefined' || value === null) {
return { error: 'No search value supplied' };
} else {
return { error: 'Invalid search value supplied' };
}
}
To concise, performant, readable, data validation: function searchEmployees(value) {
switch (xtype.which(value, 'str2+ str1 int+ obj1 obj2+ num nil')) {
case 'str2+':
return EmployeeDB.searchByName(value);
case 'str1':
return EmployeeDB.searchByMiddleInitial(value);
case 'int+':
return EmployeeDB.searchByEmployeeNumber(value);
case 'obj1':
return EmployeeDB.searchByFieldValuePair(value);
case 'obj2+':
return { error: 'Search by multiple fields not supported' };
case 'num':
return { error: 'Invalid employee number supplied' };
case 'nil':
return { error: 'No search value supplied' };
default:
return { error: 'Invalid search value supplied' };
}
} xtype(5); // 'positive_integer'
// Switch to compact name scheme:
xtype.setOptions({nameScheme:'compact'});
xtype(5); // 'int+'
See this jsbin I've made for this specific piece of code - http://jsbin.com/kixuko/1/edit?html,js,console.
This, precisely. I feel Ceylon uses more sensible defaults all around, like also having non-public methods/properties by default unless explicitly declared public. I use Kotlin in some projects, but don't really like its default of public properties and methods unless explicitly declared private, nor their decision on final classes by default.
It's also so many other little things in Ceylon that really add up to make it a pleasure to use, like its simplification of numeric types to just the minimal meaningful set of Integer and Float[1] (no Double, Long, etc), its integer literals which use the metric suffix system, like 5k for 5000, 5M for 5 million, and even hex integer literals like #ffffff, etc.[2][3], its comparison operations which return one of the enumerated Comparison type of smaller, equal, or larger (and not -1, 0, 1) ...etc
[1] https://ceylon-lang.org/documentation/current/tour/language-...
[2] https://ceylon-lang.org/documentation/current/reference/lite...
[3] https://ceylon-lang.org/documentation/current/tour/language-...