npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@feb199/result

v1.0.1

Published

Used to store log results of functions/methods and also to executes actions depending on result of functions/methods.

Downloads

48

Readme

Result

Used to store log results of functions/methods and also to executes actions depending on result of functions/methods.

Verified works on Windows.

Install

npm i @feb199/result --save

Setup

//#region Setting up ResultBase
const { Result, Results, ResultBase } = require("@feb199/result");
const logLevelsEnum = ResultBase.logLevelsEnum;
ResultBase.currentLogLevel = logLevelsEnum.INFO;
//#endregion

console.log("Test1+2");
//                     Name(String)   LogLevelType(EnumItem)     HTTPStatusCode(Number)   action(Number)         Message(String)          value(Any)
let result = new Result(  "Test"  ,     logLevelsEnum.INFO     ,           200           ,       1       , "Test completed successfully", [0, 1, 7, 3]);

//                       Name(String)   LogLevelType(EnumItem)     HTTPStatusCode(Number)   action(Number)         Message(String)
let result2 = new Result(  "Test2"  ,     logLevelsEnum.INFO     ,           400           ,       0       , "Test2 failed: user error");

result.setChild(result2);
console.log("\n\nresult.print();");
result.print();

console.log("\n\n\n\nresult.printMore();");
result.printMore();

Examples

console.log("\n\n\n\n\nTest3+4");


let result4 = new Result("Test4", logLevelsEnum.INFO, 200, 1, "Test4 completed successfully", [0, 1, 7, 3]);

result4.setParent(new Result("Test3", logLevelsEnum.INFO, 400, 0, "Test3 failed: user error"));

console.log("\n\nresult4.print();");
result4.print();

console.log("\n\n\n\nresult4.printMore();");
result4.printMore();

console.log("\n\n\n\nresult4.printMore(2);");
result4.printMore(2);
console.log("\n\n\n\n\nTest5+6+7+8");


let result5 = new Result("Test5", logLevelsEnum.INFO, 204, 1, "Test5 completed successfully")

result5.setChild(new Results("Test6", 200, 1, [
    new Result("Test7", logLevelsEnum.INFO, 400, 0, "Test7 failed: user error"),
    new Result("Test8", logLevelsEnum.INFO, 200, 1, "Test7 completed successfully", "Returned Text")
]));

console.log("\n\nresult5.print();");
result5.print();

console.log("\n\n\n\nresult5.printMore();");
result5.printMore();
console.log("\n\n\n\n\nTest9+10+testFunct*3");


function testFunct(testVar = null) {
    if(testVar === false) return new Result("Test10: testFunct", logLevelsEnum.WARN, 503, 0, "testVar is false");
    if(testVar === null) return new Result("Test10: testFunct", logLevelsEnum.WARN, 500, 0, "testVar is set to an incorrect value");
    return new Result("Test10: testFunct", logLevelsEnum.WARN, 200, 1, "testVar is set to a correct value");
}

let result9 = new Result("Test9", logLevelsEnum.INFO, 204, 1, "Test5 completed successfully")

let testFunctResults = [
    testFunct(),
    testFunct(false)
]
testFunctResults.push(testFunct(true));
result9.setChild(new Results("Test10", 204, 1, testFunctResults));


console.log("\n\nresult9.print();");
result9.print();

console.log("\n\n\n\nresult9.printMore();");
result9.printMore();
console.log("\n\n\n\n\nPractical Example:");

var numOfDice = 2;
var total = null;

function setTotalVar(inputTotal) {
    inputTotal = Number(inputTotal);
    if(isNaN(inputTotal)) return new Result("Main: setTotal", logLevelsEnum.INFO, 400, 0, "Need a number");
    if(inputTotal < numOfDice || inputTotal > numOfDice * 6) return new Result("Main: setTotal", logLevelsEnum.INFO, 400, 0, "Cannot set less than numOfDice or set higher than possible (numOfDice * 6)");
    total = inputTotal;
    return new Result("Main: setTotal", logLevelsEnum.DEBUG, 204, 1, `Set 'total' to ${inputTotal}`);
}

function totalVarCallback(totalInput) {
    let result;
    result = setTotalVar(totalInput);

    if(result.action) {
        return result.setParent(new Result("Main - total - totalCallback", logLevelsEnum.DEBUG, 204, 1, "Main totalCallback success"));
    } else {
        return result.setParent(new Result("Main - total - totalCallback", logLevelsEnum.INFO, 400, 0, "Main totalCallback failed - believed to be client error"));
    }
}

let resultMain = new Result("Main", logLevelsEnum.INFO, 204, 1, "Main executed successfully");
resultMain.setChild(totalVarCallback(5));

ResultBase.currentLogLevel = logLevelsEnum.INFO;
console.log("\n\n\nResultBase.currentLogLevel = logLevelsEnum.INFO;\nresultMain.printMore();");
resultMain.printMore();

ResultBase.currentLogLevel = logLevelsEnum.TRACE;
console.log("\n\n\nResultBase.currentLogLevel = logLevelsEnum.TRACE;\nresultMain.printMore();");
resultMain.printMore();

Documentation

.name, .type, .code, .action Variable

Name of this .name (Variable) Type String

Type of this .type (Variable) Type EnumItem

Code of this .code (Variable) Type Number

Action of this .action (Variable) Type Number

.currentLogLevel, .logLevelsEnum Static Variable

(ResultBase) = ResultBase or Result or `Results

(Static Variable) currentLogLevel of ResultBase (ResultBase).currentLogLevel Type EnumItem

(Static Variable) logLevelsEnum of ResultBase (ResultBase).logLevelsEnum Type Enum

isResult() Method

Checks if provided result in an instance of ResultBase, dosent check if(type <= currentLogLevel) isResult(result) (Method) Param result = ResultBase Returns Boolean

.child, setChild(), clearChild() (Getter, Method, Method)

Gets child of this result, dosent check if(type <= currentLogLevel) .child (Getter) Returns ResultBase

Sets child of this to param result, dosent check if(type <= currentLogLevel) .setChild(result, skipParentAssign?) (Method) Param result = ResultBase Param skipParentAssign = Boolean? Returns ResultBase

Clears child of this, dosent check if(type <= currentLogLevel) .clearChild(childAllreadyCleared?) (Method) Param childAllreadyCleared = Boolean? Returns Boolean

.parent, setParent(), clearParent() (Getter, Method, Method)

Gets parent of this result, dosent check if(type <= currentLogLevel) .parent (Getter) Returns ResultBase

Sets parent of this to param result, dosent check if(type <= currentLogLevel) .setParent(result, skipChildAssign?) (Method) Param result = ResultBase Param skipChildAssign = Boolean? Returns ResultBase

Clears parent of this, dosent check if(type <= currentLogLevel) .clearParent(parentAllreadyCleared?) (Method) Param parentAllreadyCleared = Boolean? Returns Boolean

.firstParent, .lastChild Getter

Gets first parent of this result's whole chain, dosent check if(type <= currentLogLevel) .firstParent (Getter) Returns ResultBase

Gets last child of this result's whole chain, dosent check if(type <= currentLogLevel) .lastChild (Getter) Returns ResultBase

.getAll() Method

Gets a custom amount of results(type <= currentLogLevel) .getAll(mode?, toIndex?, collapseMultiResults?, flattenMultiResults?) (Method) Param mode = Number? - 0 = Get all from first parent to last child, 1 = Progress from last child to first parent. Param toIndex = Number? Param collapseMultiResults = Boolean? Param flattenMultiResults = Boolean? Returns ResultBase

.print(), .printMore() Method

Prints this properties, dosent check if(type <= currentLogLevel) .print(onlyName?) (Method) Param onlyName = String? Returns Boolean

Print all results(type <= currentLogLevel) .printMore(mode?, numToProgress?, collapseMultiResults?) (Method) Param mode = Number? - 0 = Progress from current to last child, 1 = Progress from current to first parent, 2 = Progress from first parent to last child, 3 = Progress from last child to first parent. Param numToProgress = Number? Param collapseMultiResults = Boolean? Returns Boolean

.belowCurrentLogLevel() Method

Checks whether or not (this.type is <= ResultBase.currentLogLevel) .belowCurrentLogLevel() (Method) Returns Boolean

.localEventHandler, .globalEventHandler ((Getter, Setter), (Getter, Setter))

Get the eventEmmitter that handles events. (Emits: print(String), clear()), localEventHandler overrides static globalEventHandler. .localEventHandler (Getter) Returns EventEmitter?

Set the eventEmmitter that handles events. (Emits: print(String), clear()), localEventHandler overrides static globalEventHandler. .localEventHandler = EventHandler (Setter) Param EventHandler = EventEmitter Returns Boolean

Get the eventEmmitter that handles events. (Emits: print(String), clear()). .globalEventHandler (Getter) Returns EventEmitter?

Set the eventEmmitter that handles events. (Emits: print(String), clear()). .globalEventHandler = EventHandler (Setter) Param EventHandler = EventEmitter Returns Boolean

(Everything ResultBase Class has Result Also has.)

.message, .value Variable

Message of this .message (Variable) Type String

Value of this .value (Variable) Type Any?

(Everything ResultBase Class has Results Also has.)

.results Getter

Gets the events of this. .results (Getter) Returns ResultBase[]