niosh-string-parse
Version:
parser for string returning data type managed
296 lines • 15.9 kB
JavaScript
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– initialize
require('niosh-00-lib-primitive');
const BOOL_TRUE_STR = ["TRUE" ,"YES","ENABLED" ,"ACTIVE" ,"ON" ,"Y","1","SI","ATTIVO" ,"ACCESO","ABILITATO" ,"VERO" , "S" ];
const BOOL_FALSE_STR = ["FALSE","NO" ,"DISABLED","DISACTIVE","OFF","N","0", "DISATTIVO","SPENTO","DISABILITATO","FALSO" ];
const BOOL_ALL_STR = [...BOOL_TRUE_STR,...BOOL_FALSE_STR];
const THROW_EXCEPTION = "@•:·<{[THROW—EXCEPTION]}>·:•@";
const TH = function(x,e) {
if (typeof x == "string" && x == THROW_EXCEPTION) {
let mex = "conversion exception";
if (e instanceof Error) {
try {
let addmex = " : " + e.message + "\n\n" + e.stack + "\n";
mex += addmex;
} catch {
mex += " : additionl error on exception management";
}
}
throw new Error(mex);
}
return x;
};
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– class
class NioshStringParse {
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– exception
static get throwException() {return THROW_EXCEPTION;}
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– string
/**
* parse string as string, if wrong return d
* @param {string|String} v
* @param {any} [d]
* @returns {string|any}
*/
static string(v,d) {
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
return v.trim();
}
/**
* parse string as string, if wrong throw exception
* @param {string|String} v
* @returns {string}
*/
static stringExcept(v) { return NioshStringParse.string(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– bool
/**
* parse string as boolean, if wrong return d
* @param {string|String|boolean|Boolean} v
* @param {any} [d]
* @returns {boolean|any}
*/
static bool(v,d) {
if (v instanceof Boolean) v = (!v) ? false : true; if (typeof v == "boolean") return v;
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = v.trim().toUpperCase();
if (BOOL_ALL_STR.indexOf(v)<0) return TH(d);
return (BOOL_TRUE_STR.indexOf(v)>=0) ? true : false;
}
/**
* parse string as boolean, if wrong throw exception
* @param {string|String|boolean|Boolean} v
* @returns {boolean}
*/
static boolExcept(v) { return NioshStringParse.bool(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– number
/**
* parse string as number including NaN and Infinity, if wrong return d
* @param {string|String|number|Number} v
* @param {any} [d]
* @returns {number|any}
*/
static number(v,d) {
if (v instanceof Number) v = parseFloat(v); if (typeof v == "number") v = v.toString();
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
return parseFloat(v.trim());
}
/**
* parse string as number including NaN and Infinity, if wrong throw exception
* @param {string|String|number|Number} v
* @returns {number}
*/
static numberExcept(v) { return NioshStringParse.number(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– float
/**
* parse string as float, if wrong return d
* @param {string|String|number|Number} v
* @param {any} [d]
* @returns {number|any}
*/
static float(v,d) {
if (v instanceof Number) v = parseFloat(v); if (typeof v == "number") v = v.toString();
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = parseFloat(v.trim());
return (Number.isNaN(v)||!Number.isFinite(v)) ? TH(d) : v;
}
/**
* parse string as float, if wrong throw exception
* @param {string|String|number|Number} v
* @returns {number}
*/
static floatExcept(v) { return NioshStringParse.float(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– int
/**
* parse string as int, if wrong return d
* @param {string|String|number|Number} v
* @param {any} [d]
* @returns {number|any}
*/
static int(v,d) {
if (v instanceof Number) v = parseInt(v); if (typeof v == "number") v = v.toString();
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = parseInt(v.trim());
return (Number.isNaN(v)||!Number.isFinite(v)||!Number.isInteger(v)) ? TH(d) : v;
}
/**
* parse string as int, if wrong throw exception
* @param {string|String|number|Number} v
* @returns {number}
*/
static intExcept(v) { return NioshStringParse.int(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– natural
/**
* parse string as natural number positive integer, if wrong return d
* @param {string|String|number|Number} v
* @param {any} [d]
* @returns {number|any}
*/
static natural(v,d) {
if (v instanceof Number) v = parseInt(v); if (typeof v == "number") v = v.toString();
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = parseInt(v.trim());
return (Number.isNaN(v)||!Number.isFinite(v)||!Number.isInteger(v)||v<0) ? TH(d) : v;
}
/**
* parse string as natural number positive integer, if wrong throw exception
* @param {string|String|number|Number} v
* @returns {number}
*/
static naturalExcept(v) { return NioshStringParse.natural(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– date
/**
* parse string as date, if wrong return d
* @param {string|String|Date} v
* @param {any} [d]
* @returns {Date|any}
*/
static date(v,d) {
if (v instanceof Date) return v;
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = v.trim();
let regex = /(^([0-9]{4}-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[12][0-9]))))[Tt]([01][0-9]|2[0123]):[0-5][0-9](:[0-5][0-9](.[0-9]{1,9})?)?([Zz]|([+-])([01][0-9]|2[0123]):?[0-5][0-9])?$)|(^\s*(([0-9]{4}(-\s*|[/\s]+)(((0?[13578]|1[02])(-\s*|[/\s]+)(0?[1-9]|[12][0-9]|3[01]))|((0?[469]|11)(-\s*|[/\s]+)(0?[1-9]|[12][0-9]|30))|(0?2(-\s*|[/\s]+)(0?[1-9]|[12][0-9]))))|((((0?[13578]|1[02])(-\s*|[/\s]+)(0?[1-9]|[12][0-9]|3[01]))|((0?[469]|11)(-\s*|[/\s]+)(0?[1-9]|[12][0-9]|30))|(0?2(-\s*|[/\s]+)(0?[1-9]|[12][0-9])))(-\s*|[/\s]+)[0-9]{4}))\s*(( ([01]?[0-9]|2[0123]):\s*[0-5]?[0-9](:\s*[0-5]?[0-9](.[0-9]{1,9})?)?\s*[Zz]?\s*(([+-])([01]?[0-9]|2[0123]):?\s*[0-5]?[0-9]?)?)|( ?[Zz]\s*([+-]([01]?[0-9]|2[0123]):?\s*[0-5]?[0-9]?)?))?\s*$)/;
let match = regex.exec(v);
if (match == null) return TH(d);
regex = /^\s*((?<Y1>[0-9]{4})[^0-9]*)?0?2[^0-9]*29([^0-9]*(?<Y2>[0-9]{4}))?/;
match = regex.exec(v);
if (match != null) {
let year = parseInt(match.groups.Y1||match.groups.Y2);
if (Number.isInteger(year) && year >= 0) {
// --- verify if have february 29 for given year
let february = new Date(Date.UTC(year,2,0,0,0,0,0));
if (february.getUTCDate() != 29) {
// --- fix february 29 with 28 when not have in year
regex = /^\s*((?<Y1>[0-9]{4})[^0-9]*)?0?2[^0-9]*29([^0-9]*(?<Y2>[0-9]{4}))?/;
v = v.replace(regex,"$<Y1>$<Y2>-02-28");
}
}
}
try {
return new Date(v);
} catch(e) {
return TH(d,e);
}
}
/**
* parse string as date, if wrong throw exception
* @param {string|String|Date} v
* @returns {Date}
*/
static dateExcept(v) { return NioshStringParse.date(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– symbol
/**
* parse string as symbol, if wrong return d
* @param {string|String|symbol} v
* @param {any} [d]
* @returns {symbol|any}
*/
static symbol(v,d) {
if (typeof v == "symbol") return v;
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = v.trim();
let regex = /^\s*Symbol\((?<DATA>[\s\S]+(?=\)))\)\s*$/;
let match = regex.exec(v);
if (match==null) return TH(d);
try {
return Symbol(match.groups.DATA);
} catch(e) {
return TH(d,e);
}
}
/**
* parse string as symbol, if wrong throw exception
* @param {string|String|symbol} v
* @returns {symbol}
*/
static symbolExcept(v) { return NioshStringParse.symbol(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– bigint
/**
* parse string as bigint, if wrong return d
* @param {string|String|bigint} v
* @param {any} [d]
* @returns {bigint|any}
*/
static bigint(v,d) {
if (typeof v == "bigint") return v;
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = v.trim();
let regex = /^\s*(?<NUMBER>[0-9]+)n\s*/;
let match = regex.exec(v);
if (match==null) return TH(d);
try {
return BigInt(match.groups.NUMBER);
} catch(e) {
return TH(d,e);
}
}
/**
* parse string as bigint, if wrong throw exception
* @param {string|String|bigint} v
* @returns {bigint}
*/
static bigintExcept(v) { return NioshStringParse.bigint(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– regexp
/**
* parse string as RegExp, if wrong return d
* @param {string|String|RegExp} v
* @param {any} [d]
* @returns {RegExp|any}
*/
static regexp(v,d) {
if (v instanceof RegExp) return v;
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = v.trim();
let regex = /^\/(?<DEF>.+)\/(?<OPT>[gmiyusd]*)$/;
let match = regex.exec(v);
if (match == null) return dTH(d);
try {
return new RegExp(match.groups.DEF,match.groups.OPT||"");
} catch(e) {
return TH(d,e);
}
}
/**
* parse string as RegExp, if wrong throw exception
* @param {string|String|RegExp} v
* @returns {RegExp}
*/
static regexpExcept(v) { return NioshStringParse.regexp(v,NioshStringParse.throwException); };
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– object
/**
* parse string as object, if wrong return d
* @param {string|String|object} v
* @param {any} [d]
* @returns {object|any}
*/
static object(v,d) {
if (typeof v == "object") return v;
if (v instanceof String) v = v.toString(); if (typeof v != "string" || v.trim() == "") return TH(d);
v = v.trim();
try {
return JSON.parse(v);
} catch(e) {
return TH(d,e);
}
}
/**
* parse string as object, if wrong throw exception
* @param {string|String|object} v
* @returns {object}
*/
static objectExcept(v) { return NioshStringParse.object(v,NioshStringParse.throwException); };
//#endregion
}
//#endregion
//#region –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– export
module.exports = { NioshStringParse };
//#endregion