Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@jdeighan/base-utils

Package Overview
Dependencies
Maintainers
1
Versions
176
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jdeighan/base-utils

low level utilities

npmnpm
Version
14.0.0
Version published
Weekly downloads
22
4.76%
Maintainers
1
Weekly downloads
 
Created
Source

Using @jdeighan/base-utils

$ npm install @jdeighan/base-utils

In the interest of getting some good documentation in place, I'm going to let the unit tests serve as the most detailed documentation. Each module will have a documentation page the gives a summary of the main functions/classes/objects of the module, along with a link to the unit tests for that module

Although I do all of my development using the CoffeeScript language (coffeescript.org, install with npm install coffeescript) that doesn't mean that you need to have CoffeeScript installed to use these libraries. My projects always include both the CoffeeScript and JavaScript versions of the libraries and, in fact, you'll see that I always import the JavaScript files in my libraries.

The /utest module provides an object named u that has methods: equal - tests for deep equality notequal - tests for deep inequality truthy - tests if a value is truthy falsy - tests if a value is falsy like - tests if a hash has a key/value, but allows additional ones throws - when passed a function, tests that it throws an exception succeeds - when passed a function, tests that it doesn't throw an exception

it also exports a class named UnitTester. The u object is a UnitTester instance.

So, for example, here are a few unit tests from the base-utils library:

u.truthy notdefined(undefined)
u.falsy  notdefined(12)
u.succeeds () => pass()
u.equal    {a:1, b:2}, {a:1, b:2}
u.notequal {a:1, b:2}, {a:1, b:3}

You might note that the tests are not named. The unit tester can determine on which line the tests exists and will report that whether a test succeeds or fails, thus allowing you to locate it quickly.

In addition, you can override methods transformValue() and/or transformExpected() of the u object. These methods will be called on the parameters passed to the methods above before the test is performed, allowing you to avoid calling the same function on a series of unit tests just to test the same function.

As an example, there is a function named escapeStr() that will take a string and change TAB characters to , space characters to ˳, and newline characters to ®. You might want to test a bunch of input strings like this:

u.equal escapeStr("   XXX\n"),  "˳˳˳XXX®"
u.equal escapeStr("\t ABC\n"),  "→˳ABC®"
u.equal escapeStr("X\nX\nX\n"), "X®X®X®"
u.equal escapeStr("XXX\n\t\t"), "XXX®→→"
u.equal escapeStr("XXX\n  "),   "XXX®˳˳"

But you could also define a transformValue() method, then simplify the tests, e.g.:

(() =>
	t = new UnitTester()
	t.transformValue = (str) => escapeStr(str)

	t.equal "   XXX\n",  "˳˳˳XXX®"
	t.equal "\t ABC\n",  "→˳ABC®"
	t.equal "X\nX\nX\n", "X®X®X®"
	t.equal "XXX\n\t\t", "XXX®→→"
	t.equal "XXX\n  ",   "XXX®˳˳"
	)()

NOTE:

  • When you need to create a new variable in a unit test, it's best to wrap the test(s) in an anonymous function, then immediately call it. Since the variable is isolated from the rest of the code, you needn't worry about name clashes with variables that you might create later on.

  • In calls to the equal() and like() methods, the 1st argument is the value, and therefore transformed by the function passed to the transformValue() method, and the 2nd argument is the expected value and therefore transformed by the function passed to the transformExpected() method

  • UnitTester is a class exported by @jdeighan/base-utils/utest. Although I could have created a new class that extends UnitTester, then overridden the transformValue() method in the new class, for simple cases, I prefer to just assign a function to the transformValue property on the base class.

his project includes these libraries:

  • / - basic utilities
  • /ll-fs - low-level file system utilities
  • /source-map - use source maps to map line numbers
  • /v8-stack - get JS call stack
  • /exceptions - exception handling
  • /cmd-args - get command line arguments
  • /utest - unit test utilities
  • /prefix - handle line prefixes, e.g. indentation
  • /named-logs - save logs keyed by a name
  • /indent - handle line indentations
  • /taml - allow TABS in yaml-like strings, etc.
  • /log - advanced logging
  • /stack - manage our own call stack
  • /debug - advanced debugging
  • /state-machine - finite state machine
  • /fs - file system utilities
  • /FileProcessor - read, then rewrite files

This project includes these binaries:

Building this project

Build this project using these instructions

How to get a screen shot in Windows 11

  • Press Windows-Shift-S
  • Select an area
  • Open Paint app
  • Paste
  • Save as PNG

Keywords

javascript

FAQs

Package last updated on 09 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts