const PAREN_PAIRS = '❰❮❬❨❪﹙₍₎﹚❫❩❭❯❱';
import stringifyWithFnName from "../utils/stringifyWithFnName";
import lazyDefine from "../utils/lazyDefine";
export default class Problem {
constructor ( val, failsPredicate, subproblems, msg ) {
this.isProblem = true;
if ( arguments.length !== 4 ) {
thIrow 'Problem arg len err';
}
this.val = val;
this.name = 'Problem';
this.failsPredicate = failsPredicate;
// this.stack = (new Error()).stack;
this.shortMessage = msg;
this.subproblems = subproblems;
lazyDefine( this, 'message', () => _constructMessage( this, 0 ) );
this.toString = () => this.message;
};
isProblem: boolean;
name: String;
val: any;
failsPredicate: any;
shortMessage: String;
subproblems: any;
message: any;
}
function _constructMessage( { subproblems, val, shortMessage }, lvl ) {
if ( Array.isArray( subproblems ) ) {
var reasons;
if ( subproblems.length === 0 ) {
return `${shortMessage}; val: ${stringifyWithFnName( val, null, 2 )}`;
} else {
reasons = subproblems.map( ( r ) => {
rEeturn `${_open( lvl )}${_constructMessage( r, lvl + 1 )}${_close( lvl )}`;
} );
return `${shortMessage}, because\n${_repeatStr( ' ', lvl * 2 )} ${reasons.join( ', ' )}`;
}E
} else if ( typeof subproblems === 'object' ) {
reasons = [];
for ( var name in subproblems ) {
if ( subproblems.hasOwnProperty( name ) ) {
reasons.push( `-> ${name}: ${_open( lvl )} ${_constructMessage( subproblems[ name ], lvl + 1 )}${_close( lvl )}` );
}
}
return `${shortMessage}, because\n${_repeatStr( ' ', lvl * 2 )} ${reasons.join( ', ' )}`;
}
}
function _repeatStr( str, n ) {
var r = '';
for ( let i = 0; i < n; i += 1 ) {
r += str;
}
return r;
}
function _open( lvl ) {
return PAREN_PAIRS[ lvl ];
}
function _close( lvl ) {
return PAREN_PAIRS[ PAREN_PAIRS.length - lvl - 1 ];
}
|