dictate
Version:
Dictate the order of the array.
132 lines (109 loc) • 3.54 kB
JavaScript
;
/*;
@module-license:
The MIT License (MIT)
@mit-license
Copyright (@c) 2017 Richeve Siodina Bebedor
@email: richeve.bebedor@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@end-module-license
@module-configuration:
{
"package": "maxelm",
"path": "maxelm/maxelm.js",
"file": "maxelm.js",
"module": "maxelm",
"author": "Richeve S. Bebedor",
"eMail": "richeve.bebedor@gmail.com",
"contributors": [
"John Lenon Maghanoy <johnlenonmaghanoy@gmail.com>",
"Vinse Vinalon <vinsevinalon@gmail.com>"
],
"repository": "https://github.com/volkovasystems/maxelm.git",
"test": "maxelm-test.js",
"global": true
}
@end-module-configuration
@module-documentation:
Returns the maximum element of the array based on the condition.
Default condition test numbers.
@end-module-documentation
@include:
{
"doubt": "doubt",
"falzy": "falzy",
"raze": "raze",
"stringe": "stringe"
}
@end-include
*/
const doubt = require( "doubt" );
const falzy = require( "falzy" );
const raze = require( "raze" );
const stringe = require( "stringe" );
const maxelm = function maxelm( array, condition ){
/*;
@meta-configuration:
{
"array:required": "[*]",
"condition": "function"
}
@end-meta-configuration
*/
if( !doubt( array, AS_ARRAY ) ){
throw new Error( "invalid array" );
}
if( falzy( condition ) ){
condition = ( previous, next ) => {
if( typeof previous == "number" && typeof next == "number" ){
return Math.max( previous, next );
}
throw new Error( `invalid element, ${ previous }, ${ next }` );
};
}
if( typeof condition != "function" ){
throw new Error( "invalid condition" );
}
try{
let maximum = raze( array ).reduce( ( previous, next ) => condition( previous, next ) );
return {
"toString": function toString( ){
return stringe( maximum );
},
"valueOf": function valueOf( ){
return maximum;
},
/*;
@note:
If the condition returns the value means that the value is greater than
the current maximum value from the array, otherwise it could be
equal or less than.
@end-note
*/
"compare": function compare( value ){
try{
return condition( maximum, value ) === value;
}catch( error ){
throw new Error( `cannot compare invalid value, ${ value }, ${ error.stack }` );
}
}
};
}catch( error ){
throw new Error( `cannot get maximum element, condition failed, ${ error.stack }` );
}
};
module.exports = maxelm;