Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Awesome js utils including - plan: An Asynchronous control flow with a functional taste - Chrono: record and visualize timelines in the console
javascript awesome utilities and extensions. Taking javascript to the next level.
NodeJS:
npm install js-awe
100% compatible with bun:
bun install js-awe
Includes consoleTable([{a:1,b2},{a:2,b:3}])
an equivalent implementatation of node console.table() producing similar visualization.
Web:
Grab the minified version at:
https://raw.githubusercontent.com/josuamanuel/js-awe/main/dist/js-awe.min.js
Then copy it in your project and work with it in your html:
...
<body>
<script type="module">
import { firstCapital} from './js-awe.min.js'
document.write(firstCapital("library is loaded correctly..."));
</script>
</body>
...
Async await has done a lot to improve the readability of code when compared with callbacks style. But sometimes it is not a good construct, especially if you want to use it in a functional style!!!
One problem I see, is the spread of async await around the source code wherever it is handy. This casual handling of await usually makes code non performant.
Every-time we use await, an async branch will be created. The result will be an execution flow with the shape of a tree, with some chain functions running in sequence and others running concurrently.
js-awe library has a “plan” function that can help you with that.
“plan” tries to solve this problem by declaring this tree explicitly in one place and in a simple elegant way. It uses array nesting to define this tree. It does not use weird DSL. “plan” is an execution planner that pipe functions to run in sequence and functions to run concurrently. It handles for you the promise chaining and data passing so you can write pure functions free of async await.
The construct to run in sequence:
[ fun1, fun2, fun3 ]
execution and data flow coincides:
fun1 -> fun2 -> fun3
* fun1 receives all the parameters when running the plan:
plan().build(...)(param1, param2)
* fun2 receives output from fun1
* fun3 receives output from fun2
The construct to run concurrently:
[ fun1, [fun2], [fun3], fun4 ]
execution and data flow coincides:
|-> fun2 --|
fun1 --| |-> fun4
|-> fun3 --|
* fun1 receives all the parameters when running the plan:
plan().build(...)(param1, param2)
* fun2 receives output from fun1
* fun3 receives output from fun1
* fun4 receives an array that contains two values:
[outputFromfun2, outputFromFun3]
The best thing is to view an example. First, we need to install it:
npm install js-awe
You can see below is a simple example of its use. This could be part of an API to get the bank balances of all the holdings (savings and loans) for a specific customer:
import { plan } from 'js-awe'
const getCustomerBalances = plan().build([
fetchAccounts,
[filterSavings, getSavingBalances],
[filterLoans, getLoanBalances],
format,
])
console.log('result: ', await getCustomerBalances('0396d9b0'))
Execution:
|->filterSavings -> getSavingBalances -|
getAccounts-| |-> format
|->filterLoans -> getLoanBalances -|
The flow of data:
You can see the whole example here:
import { plan } from 'js-awe'
const getCustomerBalances = plan().build([
fetchAccounts,
[filterSavings, getSavingBalances],
[filterLoans, getLoanBalances],
format,
])
console.log('result: ', await getCustomerBalances('0396d9b0'))
function filterSavings(accounts) {
return accounts.filter((account) => account.type === 'saving')
}
function getSavingBalances(savingAccounts) {
const listOfAcccountsToFetch = savingAccounts.map((account) => account.id)
return fetchSavingBalances(listOfAcccountsToFetch)
}
function filterLoans(accounts) {
return accounts.filter((account) => account.type === 'loan')
}
function getLoanBalances(loanAccounts) {
const listOfAcccountsToFetch = loanAccounts.map((account) => account.id)
return fetchLoanBalances(listOfAcccountsToFetch)
}
function format([savingBalances, loanBalances]) {
return [...savingBalances, ...loanBalances]
}
// Data fetch services are mocked for local running.
// In production they should be fetch APIs to real implementations.
function fetchAccounts(customerId) {
return Promise.resolve([
{ id: 1, type: 'saving' },
{ id: 2, type: 'loan' },
])
}
function fetchSavingBalances(listOfAcccountsToFetch) {
return Promise.resolve([
{
id: 1,
type: 'saving',
balance: 13,
},
])
}
function fetchLoanBalances(listOfAcccountsToFetch) {
return Promise.resolve([
{
id: 2,
type: 'loan',
balance: 24,
},
])
}
The Plan utility is recommended when we have a complex tree, and you want to manifest explicitly this async flow. For example, This utility would be a good tool for an API that generates its response based on different calls to other APIs. Especially if some of the calls need to be called in sequence and others can be run concurrently.
When it is not recommended:
Chrono time events and visualize them.
Spread in your Async code some:
chrono.time('step1')
chrono.timeEnd('step1')
And then chrono.report()
to log the timeline and some stats.
Full example: chronoExample.js
chronoCreation : 2023-05-25T20:58:17.175Z
report : 2023-05-25T20:58:18.480Z
Timeline of events:
┌────────┬───────────────────────────────────────────────────────────────────────────────────────┐
│ Events │ ms 0 650 1186 1288 │
├────────┼───────────────────────────────────────────────────────────────────────────────────────┤
│ step1 │ |--------------------------------------| || │
│ step2 │ |-------------------------------------| │
│ step3 │ |-----------------------------| │
└────────┴───────────────────────────────────────────────────────────────────────────────────────┘
Total elapse Time of each event:
┌─────────┬─────────┬────────┬────────────┐
│ (index) │ name │ elapse │ percentage │
├─────────┼─────────┼────────┼────────────┤
│ 0 │ 'step1' │ 650 │ 36.81 │
│ 1 │ 'step2' │ 615 │ 34.84 │
│ 2 │ 'step3' │ 501 │ 28.36 │
└─────────┴─────────┴────────┴────────────┘
Coinciding Events timeline:
┌─────────┬──────────────────────┬──────────┬────────────┐
│ (index) │ runningEvents │ elapseMs │ percentage │
├─────────┼──────────────────────┼──────────┼────────────┤
│ 0 │ [ 'step1' ] │ 650 │ 51.38 │
│ 1 │ [ 'step2' ] │ 114 │ 9.04 │
│ 2 │ [ 'step2', 'step3' ] │ 501 │ 39.58 │
└─────────┴──────────────────────┴──────────┴────────────┘
For you to explore... Don't hesitate to explore the code: table.js, sanitizer.js, jsUtils.js...
Most of the functions has TS types. But more documentation will be needed. So if you want to help others:
please PULL REQUEST!!! with your changes.
The library offer typescript types in ./types/ for the consumer. Currently only the main functions are typed in detail.
To generate types:
tsc -p tsconfig.json
This will output types in ./genTypes then we will need to copy the new type definitions into ./types/ . This is to keep the documentation that was already manually generated.
Change the version of the library in package.json version field. npm forces that each version published to have a unique version value.
npm publish
FAQs
Awesome js utils including - plan: An Asynchronous control flow with a functional taste - Chrono: record and visualize timelines in the console
The npm package js-awe receives a total of 366 weekly downloads. As such, js-awe popularity was classified as not popular.
We found that js-awe demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.