help-node js
A simple module that provides utility for implements sequence of operation via promises, managment errors, debugging, testing purposes YET A DRAFT README...
Installation
npm install --save help-nodejs
Motivation
The goal of this library is to simplify some tasks, for example the serial execution of hybrid functions (async, sync , promises) , the managment of errors. I wrote to simplify these tasks in my project :
Sequence of operation
Often you'll need to do sequences of operations. The classical way to do this is by async.waterfall :
async
Well, this is a great approach to avoid the "hell of waterfall if then else" :
or p
p.sequence accept and execute a list of promises that you can convert from sync and async functions with p.fromSync, p.fromAsync. These functions accept an array of arguments. Boring because too much code? Well, use the seqMaker func:
var makerSeq = p makerSeq start
Now your code is more compact and simpler, like a sequential code.
Data
Many times you need to load an array with arguments of same type, and you've to search them by an identificator (id or name, for example). data.js offers a special type of array : Objects. You can set a condition function that must returns true if the object you're trying to insert in the array is of correct type. For example, suppose that you want an array of objects with name and color properties:
var objects = data objects objects assert assert assert //objects.add({color:'p'}) assert
The setOptions function sets some options for the array. If you set exception.add to true, when you're trying to add an object that doesn't match the schema defined in the condition function Objects will throw an exception, else simply returns. The same for the "uniq": Objects doesn't accept two equal objects, if you try to add an object already loaded it won't allow you (and it will throw an exception if you set exception.uniq to true) Use the get function to find the loaded objects :
var ele = [ {id:1, name:"pippo" , color:"blue" } , {id:2, name:"jack" ,color:"red" } ] var objects = data.Objects() objects.addAll(ele) assert.equal(ele[0], objects.get({color:"blue"}),"should give blue") assert.equal(ele[1], objects.get({color:"red"}), "should give red")
print_dbg
If you're writing an async function you'll need to test the return, sometimes with the following way:
{ iferr console console else console console }
well, print_dbg implement this simple if then else, so you'll do this :
const pd = print_dbgfs
Strings
Some simple utility function to manage strings:
- How much occurrences of a substring in a string?
javascript occurrences(string, subString, allowOverlapping)
- substring inside two words / character
javascript substring(str, first, second)
- Add a string after the first ( of all) occurrences
* Remove a substring from a string :
javascript remove(str, toRemove, global)
Error managment
You should create a set of errors for your application, and ofter you'll map a set of backhend errors with a subset of front-end errors (you shouldn't show to user that a "SQL Insert fail", but a user-friendly message, and the same with check controls). You should give an error message and it's a good practice to add a code for the messages. I decided to not reinvent the wheel, there's a wonderful module that do this: https://www.npmjs.com/package/errors. So, I've created a simple module that collect a series of conditions. You load a condition with
where name is a string, conditionFn is a function that returns a boolean (it implements a specific condition that you want to load), error is the error that will be thrown (or sent to callback) if the check function fails. The check function check a specific argument:
An example of use is this:
const checker = require('help-nodejs').checker
var c = checker.Checker()
c.load("simple", (e) => {
return e > 10
}
, new Error("arg must be > 10"))
//Sync mode
assert.throws(() => c.check(9, "simple"), "Should throw exc")
//With callback
c.check(9, "simple", (err) => { assert.notEqual(err, null, "Error should not be null")} )
c.check(11, "simple", (err) => { assert.equal(err, null, "Error should be null")} )
//c.load("Numberic", c.JoiCondition(Joi.number()), Joi.number().error)
assert.end()
(If you want a promise, you can use the fromAsync function of promises) You'll know the wonderful Joi library... if you don't know, do it now! checker offers a specific JoiChecker type that you can use to set JoiChecker conditions:
http-helper
A module that provides utility to manage the http responses in a web application. When you process a user request you'll have to response. Http-helper provides a standard way to do this: when the elaboration is done, simply call response(res, err, results) : if some error occurred http-helper will send an http error in the http header, and the error message and code that caused the problem. If no errors occcurs it will send a 200 ok reply with results in a json object. The code is straightforward:
{ res; var output = error: null data: data ; res;} { var code = errcode ? errcode : errname; res; res; } { iferr ; else ; }
Obviously the application errors aren't http errors... so http-helper provides the register method, that accepts an error and an http code. Allowed code are :
const httpCodes = BAD_REQUEST: 400 UNAUTHORIZED: 401 PAYMENT_REQUIRED: 402 FORBIDDEN: 403 NOT_FOUND: 404 METHOD_NOT_ALLOWED: 405 NOT_ACCEPTABLE: 406 PROXY_AUTHENTICATION_REQUIRED: 407 REQUEST_TIMEOUT: 408 CONFLICT: 409 GONE: 410 LENGTH_REQUIRED: 411 PRECONDITION_FAILED: 412 PAYLOAD_TOO_LARGE: 413 URI_TOO_LONG: 414 UNSUPPORTED_MEDIA_TYPE: 415 RANGE_NOT_SATISFIABLE: 416 EXPECTATION_FAILED: 417 IM_A_TEAPOT: 418 MISDIRECTED_REQUEST: 421 UNPROCESSABLE_ENTITY: 422 LOCKED: 423 FAILED_DEPENDENCY: 424 UNORDERED_COLLECTION: 425 UPGRADE_REQUIRED: 426 PRECONDITION_REQUIRED: 428 TOO_MANY_REQUESTS: 429 REQUEST_HEADER_FIELDS_TOO_LARGE: 431 UNAVAILABLE_FOR_LEGAL_REASONS: 451 INTERNAL_SERVER_ERROR: 500 NOT_IMPLEMENTED: 501 BAD_GATEWAY: 502 SERVICE_UNAVAILABLE: 503 GATEWAY_TIMEOUT: 504 HTTP_VERSION_NOT_SUPPORTED: 505 VARIANT_ALSO_NEGOTIATES: 506 INSUFFICIENT_STORAGE: 507 LOOP_DETECTED: 508 BANDWIDTH_LIMIT_EXCEEDED: 509 NOT_EXTENDED: 510 NETWORK_AUTHENTICATION_REQUIRED: 511
If an error occurred and you don't register it in http-helper, it will be sent a default 503 error. For example, let's suppose that you want to send a 501 error code if the user go in a section of code that you've not already implemented. In configuration section of your application you do this :
httpHelper = httpHelper//Define the errors (you should use a managment module in order to define types of errors such the errors module)var errorNoImplement = "NO IMPLEMENTED" //Register the errorhttpHelper
When the user goes in the wrong code do this
httpHelper = httpHelper { ifsomething else } {//User called this for some reason }... // In callback {//This will send an error with http code 500 for the browser , and particular error in the body of response (with a code or the name of Error class if you don't set a code to the error instance, and the message of the error) , so you can manage it client-sidehttpHelper }
Tests
With tape, run
npm test
Contributors
If you have any idea to implement some utility function for node, checkout the branch and send me a pull request!
License
MIT Licensed