ezito-utils
Advanced tools
Comparing version 1.2.2 to 2.2.0
'use strict'; | ||
const returnTypes = { | ||
context : undefined, | ||
frist : undefined, | ||
last : undefined, | ||
length : undefined, | ||
const ArrayToNodeList = function (list = [] ) { | ||
/** | ||
* @param {number} index | ||
* @returns {Node} | ||
*/ | ||
item ( index ) { | ||
return this[+index || 0].value; | ||
}, | ||
/** | ||
* @param {function} callback | ||
* @returns {undefined} | ||
*/ | ||
each (callback ){ | ||
var next = false; | ||
function stop(){ | ||
next = true ; | ||
} | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
callback(this[item].value , stop); | ||
if(next) break; | ||
} | ||
} | ||
}, | ||
/** | ||
* @param {function} callback | ||
* @returns {returnTypes} | ||
*/ | ||
map(callback){ | ||
var arr = []; | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
arr.push(callback(this[item].value)); | ||
} | ||
} | ||
return ArrayToNodeList(arr); | ||
}, | ||
/** | ||
* @param {function} callback | ||
* @returns {returnTypes} | ||
*/ | ||
filter(callback){ | ||
var arr = []; | ||
var is ; | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
is = callback(this[item].value); | ||
if(is) arr.push(this[item].value); | ||
} | ||
} | ||
return ArrayToNodeList(arr); | ||
}, | ||
/** | ||
* @param {function} callback | ||
* @returns {returnTypes} | ||
*/ | ||
values(callback){ | ||
var arr = []; | ||
if(typeof callback === "function" ){ | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
callback(this[item].value.value , this[item].value ); | ||
} | ||
} | ||
} | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
arr.push(this[item].value.value); | ||
} | ||
} | ||
return ArrayToNodeList(arr); | ||
}, | ||
/** | ||
* @param {function} callback | ||
* @returns {object} | ||
*/ | ||
valuesWithKey(callback){ | ||
var key ; | ||
var result = {}; | ||
var value ; | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
value = this[item].value.value; | ||
if(typeof callback == "function"){ | ||
key = callback(value , this[item].value); | ||
result[key] = value; | ||
} | ||
if(callback instanceof Array){ | ||
key = callback.pop(); | ||
result[key] = value; | ||
} | ||
} | ||
} | ||
return result | ||
}, | ||
/** | ||
* @param {function} callback | ||
* @returns {object} | ||
*/ | ||
joinWithKey(callback){ | ||
var key ; | ||
var result = {}; | ||
for (var item in this){ | ||
if(Number(item) >= 0){ | ||
if(typeof callback == "value"){ | ||
key = callback(this[item].value); | ||
result[key] = this[item].value; | ||
} | ||
if(callback instanceof Array){ | ||
key = callback.pop(); | ||
result[key] = this[item].value; | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
}; | ||
/** | ||
* @param {Array} list | ||
* @returns {returnTypes}; | ||
*/ | ||
function ArrayToNodeList (list = [] ) { | ||
var obj_return = {}; | ||
list.map( (x, xi) => obj_return[xi] = { value: x } ); | ||
obj_return.item = { value: function (i) { return this[+i || 0] } }; | ||
list.map((x, xi) => obj_return[xi] = { value: x } ); | ||
obj_return.frist = { value: list[0] }; | ||
obj_return.last = { value: list[list.length - 1 ] }; | ||
obj_return.length = { value: list.length }; | ||
obj_return.each = { | ||
value : function ( fn ){ | ||
list.forEach(fn); | ||
} | ||
} | ||
obj_return.item = { value: returnTypes.item.bind(obj_return) }; | ||
obj_return.each = { value : returnTypes.each.bind(obj_return) }; | ||
obj_return.forEach = obj_return.each; | ||
obj_return.values = { value : returnTypes.values.bind(obj_return) }; | ||
obj_return.filter = { value : returnTypes.filter.bind(obj_return) }; | ||
obj_return.joinWithKey = { value : returnTypes.joinWithKey.bind(obj_return) }; | ||
obj_return.map = { value : returnTypes.map.bind(obj_return) }; | ||
obj_return.valuesWithKey = { value : returnTypes.valuesWithKey.bind(obj_return) }; | ||
return Object.create(document.createDocumentFragment().childNodes , obj_return ); | ||
@@ -17,0 +175,0 @@ }; |
require('ezito-utils/client/node-prototype'); | ||
const ArrayToNodeList = require('ezito-utils/client/array-to-node-list'); | ||
const NodeAttrToCssSelector = require('ezito-utils/client/node-attr-to-css-selector'); | ||
const returnTypes = require('ezito-utils/client/array-to-node-list/types'); | ||
/** | ||
* @param {<Node ,Element , String >} node | ||
* @param {String} query | ||
* @param {...any} children | ||
* @returns {returnTypes} | ||
*/ | ||
const Query = function ( node , query = null , ...children ) { | ||
@@ -6,0 +15,0 @@ // check if node is function return window load event |
{ | ||
"name": "ezito-utils", | ||
"version": "1.2.2", | ||
"version": "2.2.0", | ||
"description": "ezito utils help you for faster programming", | ||
@@ -5,0 +5,0 @@ "main": " ", |
@@ -10,6 +10,6 @@ "use strict"; | ||
if (context.length != array.length) return false; | ||
for (var i = 0, l=context.length; i < l; i++) { | ||
for (var i = 0, l = context.length; i < l; i++) { | ||
// Check if we have nested arrays | ||
if (context[i] instanceof Array && array[i] instanceof Array) { | ||
if (!context[i].equals(array[i])) return false; | ||
if (!equals(context[i] , array[i])) return false; | ||
} | ||
@@ -20,6 +20,7 @@ else if (context[i] != array[i]) { | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
module.exports = equals ; |
@@ -12,3 +12,3 @@ 'use strict'; | ||
module.exports.request = function (obj){ | ||
return obj instanceof http.ClientRequest; | ||
return obj instanceof http.IncomingMessage; | ||
} |
@@ -7,2 +7,3 @@ 'use strict'; | ||
const isBoolean = require('ezito-utils/public/is/number'); | ||
const isArray = require('ezito-utils/public/is/array'); | ||
/** | ||
@@ -20,2 +21,3 @@ * | ||
!isNumber(param) && | ||
!isArray(param) && | ||
param !== NaN | ||
@@ -22,0 +24,0 @@ ); |
@@ -10,6 +10,5 @@ "use strict"; | ||
*/ | ||
function has(object , key , valueType = undefined ){ | ||
if('object' !== typeof object) return false; | ||
if(object.hasOwnProperty(key) === true){ | ||
function has(object , key , valueType = undefined ){ | ||
if('object' !== typeof object) return false; | ||
if(Object.prototype.hasOwnProperty.call(object , key) === true){ | ||
if( typeof valueType === "function"){ | ||
@@ -16,0 +15,0 @@ return valueType(object[key]) |
@@ -0,7 +1,6 @@ | ||
'use strict'; | ||
/** | ||
* | ||
*/ | ||
const getBrowers = require('ezito-utils/client/get-browers'); | ||
var haveCaptureStackTrace = 'captureStackTrace' in Error; | ||
@@ -51,2 +50,3 @@ function FireFoxStackParser(error , hasName){ | ||
}; | ||
function V8StackParser (error){ | ||
@@ -105,2 +105,3 @@ function CallSite({ functionName , lineNumber , fileName , charStart }){ | ||
} | ||
function errorConfig(){ | ||
@@ -243,11 +244,24 @@ if(!haveCaptureStackTrace && !('captureStackTrace' in Error)){ | ||
} | ||
stackTrace.createErrorOption = function (stackNumber = 1 , message ){ | ||
function createErrorOption(stackNumber = 1 , message = '' , code = 1){ | ||
var trace = stackTrace(stackNumber + 1); | ||
return { | ||
message, | ||
code, | ||
fileName : trace.getFileName(), | ||
line : trace.getLineNumber(), | ||
lineNumber : trace.getLineNumber(), | ||
functionName : trace.getFunctionName(), | ||
methodName : trace.getMethodName(), | ||
columnNumber : trace.getColumnNumber(), | ||
} | ||
} | ||
Object.defineProperty(stackTrace , "createErrorOption" , { | ||
writable : false, | ||
configurable : false, | ||
value : createErrorOption.bind(stackTrace), | ||
}) | ||
module.exports = stackTrace |
'use strict'; | ||
const isString = require('ezito-utils/public/is/string'); | ||
@@ -13,3 +14,3 @@ const isFunction = require('ezito-utils/public/is/function'); | ||
const isSymbol = require('ezito-utils/public/is/symbol'); | ||
var isPromise = require('ezito-utils/public/is/promise'); | ||
const isPromise = require('ezito-utils/public/is/promise'); | ||
@@ -19,18 +20,48 @@ | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.string = function stringCheck (param){ | ||
return isString(param); | ||
}; | ||
/** | ||
* @param {any} object | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.promise = function (object){ | ||
return isPromise(object); | ||
} | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.function = function functionCheck(param){ | ||
return isFunction(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.class = function (param){ | ||
return isClass(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.boolean = function (param){ | ||
return isBoolean(param); | ||
}; | ||
EzitoTypes.array = function (param , validator ){ | ||
/** | ||
* @param {any} param | ||
* @param {function} validator | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.array = function (param , validator){ | ||
function Validator(_args , _validator){ | ||
@@ -46,3 +77,3 @@ for (const iterator of param) { | ||
else { | ||
if(isFunction(_validator) && _validator(iterator)) return false; | ||
if(isFunction(_validator) && !_validator(iterator)) return false; | ||
} | ||
@@ -57,20 +88,48 @@ } | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.number = function (param){ | ||
return isNumber(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.arrowFunction = function (param){ | ||
return isArrowFunction(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.asyncFunction = function (param){ | ||
return isAsyncFunction(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.object = function objectCheck(param){ | ||
return isObject(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.symbol = function objectCheck(param){ | ||
return isSymbol(param); | ||
}; | ||
/** | ||
* @param {any} param | ||
* @returns {boolean} | ||
*/ | ||
EzitoTypes.undefined = function objectCheck(param){ | ||
return typeof param === "undefined" || param === undefined; | ||
}; | ||
/** | ||
* @param {...any} args | ||
* @returns {function} | ||
*/ | ||
EzitoTypes.oneOfType = function oneOfTypeCheck(...args){ | ||
@@ -77,0 +136,0 @@ return function oneOfCheckCreator(param) { |
@@ -6,3 +6,2 @@ 'use strict'; | ||
const trace = require('ezito-utils/public/trace'); | ||
const isArray = require( "ezito-utils/public/is/array"); | ||
@@ -15,9 +14,12 @@ module.exports = function ( fn ){ | ||
return function (req ,res ,next){ | ||
return function middleware(req ,res ,next){ | ||
if(!isHttp.request(req)){ | ||
throw (trace.createErrorOption(1,"req param must be have http request type")) | ||
throw trace.createErrorOption(1,"req param must be have http request type"); | ||
} | ||
if(!isHttp.response(res)){ | ||
throw (trace.createErrorOption(1,"res param must be have http response type")) | ||
throw trace.createErrorOption(1,"res param must be have http response type"); | ||
} | ||
const type = req.method.toLowerCase(); | ||
@@ -29,4 +31,5 @@ if(type !== 'post') { | ||
} | ||
return fn.call( this , req , res , next ); | ||
} | ||
} |
@@ -1,146 +0,7 @@ | ||
'use strict'; | ||
'use strict'; | ||
const body = require("./core/body"); | ||
const header = require("./core/header"); | ||
/** | ||
* | ||
* @param {string} pName | ||
* @returns | ||
*/ | ||
const body = function(pName){ | ||
const name = String(pName).toLowerCase() ; | ||
const config = { | ||
msg : undefined , | ||
length : undefined , | ||
email : undefined , | ||
phone : undefined , | ||
notEmpty : undefined , | ||
number : undefined , | ||
}; | ||
const validator = { | ||
length : ( value ) => { | ||
if(typeof config.length === "function" ){ | ||
return config.length.call( this , value ); | ||
} | ||
else if( typeof config.length === "object" ){ | ||
let is = true ; | ||
if(config.length.min && Number(config.length.min)){ | ||
is = String(value).length >= config.length.min ; | ||
} | ||
if(config.length.max && Number(config.length.max) && is ){ | ||
is = String(value).length <= config.length.max ; | ||
} | ||
return is; | ||
} | ||
return String(value).length === config.length ; | ||
} , | ||
email : (email) => { | ||
return String(email).toLowerCase().match( | ||
/^(([^<>()[\]\\.,;:\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,}))$/ | ||
); | ||
}, | ||
number : function(value){ | ||
let is = !!Number(value); | ||
if(typeof config.length === "function" ){ | ||
return config.length.call( this , value ); | ||
} | ||
else if( typeof config.length === "object" ){ | ||
if(config.length.min && Number(config.length.min) && is){ | ||
is = value >= config.length.min ; | ||
} | ||
if(config.length.max && Number(config.length.max) && is ){ | ||
is = value <= config.length.max ; | ||
} | ||
} | ||
return is; | ||
}, | ||
phone : function(reqular){ | ||
}, | ||
notEmpty : function( value ){ | ||
return String(value).trim() !== ""; | ||
}, | ||
}; | ||
const fn = function(req,res,next){ | ||
try { | ||
if(!req.validator){ | ||
Object.defineProperty( req , 'validator' , { | ||
value : { | ||
error : [] , | ||
done : true , | ||
} , | ||
writable : true | ||
}); | ||
} | ||
if(!req.body[name]){ | ||
req.validator.error.push({ | ||
name , | ||
message : config.msg | ||
}); | ||
} | ||
else { | ||
var nValue = req.body[name]; | ||
Object.entries(config).forEach(([ key , value])=>{ | ||
if(value){ | ||
let is = validator[key] ? validator[key].call( this , nValue ) : true ; | ||
if(!is){ | ||
req.validator.error.push({ | ||
name , | ||
message : config.msg | ||
}); | ||
req.validator.done = false ; | ||
} | ||
} | ||
}); | ||
}; | ||
} catch (error) { | ||
req.validator.error.push({ | ||
name : "unknow" , | ||
}) | ||
}; | ||
if(req.validator.error.length){ | ||
req.validator.done = false; | ||
} | ||
next(); | ||
}; | ||
/** | ||
* | ||
* @param {string} message | ||
* @returns | ||
*/ | ||
fn.withMessage = function(message){ | ||
config.msg = message; | ||
return fn; | ||
}; | ||
/** | ||
* | ||
* @param {Number | Object.min & Object.max } length | ||
* @returns | ||
*/ | ||
fn.withLength = function(length){ | ||
config.length = length ; | ||
return fn; | ||
}; | ||
fn.isEmail = function(){ | ||
config.email = true; | ||
return fn; | ||
}; | ||
fn.isNumber = function(){ | ||
config.number = true ; | ||
return fn; | ||
} | ||
fn.notEmpty = function(){ | ||
config.notEmpty = true; | ||
return fn; | ||
}; | ||
return fn ; | ||
}; | ||
/** | ||
@@ -178,3 +39,3 @@ * | ||
if( req.validator && req.validator.error instanceof Array ){ | ||
req.validator.error.push( | ||
req.validator.errors.push( | ||
{ | ||
@@ -214,7 +75,43 @@ name , | ||
const methodType = function (typeName){ | ||
return function ( req ,res , next) { | ||
var mt = req.method.toLowerCase(); | ||
if(!req.validator){ | ||
Object.defineProperty( req , 'validator' , { | ||
value : { | ||
errors : [] , | ||
done : true , | ||
} , | ||
writable : true | ||
}); | ||
} | ||
if(mt !== typeName.toLowerCase()){ | ||
req.validator.errors.push({ | ||
["method-type"] : typeName , | ||
message : "request method type error" | ||
}); | ||
} | ||
next(); | ||
} | ||
}; | ||
const check = function(req){ | ||
if(req && req.validator && req.validator.error.length > 0 ) return req.validator.error; | ||
if(req && req.validator && req.validator.errors.length > 0 && req.validator.done === false) return req.validator.error; | ||
return false; | ||
}; | ||
const mergeValidators = function ( validatorList ){ | ||
@@ -225,4 +122,3 @@ if(!( validatorList instanceof Array )) throw new Error('param error'); | ||
validatorList.map(function(validator){ | ||
if(!( typeof validator === "function" )) throw new Error('validator error'); | ||
if(!( typeof validator === "function" )) throw new Error('validator error'); | ||
validator(req,null,()=>{}); | ||
@@ -234,4 +130,12 @@ }); | ||
/** | ||
* | ||
/** | ||
* @param {Array} validatorList | ||
@@ -246,7 +150,6 @@ * @returns | ||
return function(req,res,next){ | ||
return function(req , res , next){ | ||
validatorList.map(function(validator){ | ||
if(!( typeof validator === "function" )) throw new Error('validator error'); | ||
validator(req,null,()=>{}); | ||
if(!(typeof validator === "function" )) throw new Error('validator error'); | ||
validator(req, null , ()=>{}); | ||
}); | ||
@@ -257,2 +160,6 @@ fn(req,res,next) | ||
}; | ||
/** | ||
@@ -277,2 +184,2 @@ * example | ||
module.exports = { body , files , check , mergeValidators , createValidator }; | ||
module.exports = { body , methodType, files , check , mergeValidators , createValidator , header }; |
const Cookies = require('cookies'); | ||
const { default : DeCrypte } = require('ezito/utils/server/crypto/de'); | ||
const { default : EnCrypte } = require('ezito/utils/server/crypto/en'); | ||
/** | ||
* | ||
const { default : DeCrypte } = require('ezito-utils/server/crypto/de'); | ||
const { default : EnCrypte } = require('ezito-utils/server/crypto/en'); | ||
/** | ||
* @param {Function} fn | ||
@@ -11,3 +11,3 @@ * @param {Array} keys | ||
module.exports = function ( fn , keys = undefined ){ | ||
return function ( req ,res , next ){ | ||
return function ( req , res , next ){ | ||
keys = Boolean(keys) === false ? ['keyboard cat'] : keys ; | ||
@@ -63,4 +63,4 @@ var cookies = new Cookies( req , res, { keys }); | ||
}) | ||
return fn.call( this , req ,res , next ); | ||
return fn.call( this , req ,res , next); | ||
} | ||
} |
804373
242
21302
51