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

jlafer-fnal-util

v1.0.5

Published

My functional programming helpers

Downloads

2

Readme

jlafer-fnal-util

This is a collection of utility functions that I find useful when working with objects, arrays and dates.

NOTE: documentation of functions to follow. So...if you stumbled across this package and were planning on using it - buyer beware. Perhaps better is to look at the source on GitHub and see/copy the code.

Installation

npm install --save jlafer-fnal-util

Helper Functions

kvListToObj

kvListToObj :: (string, string) -> [object] -> object

This HOF takes two strings -- representing the keys of property keys and their values -- and will create a function that transforms an array of such key-value objects into an object with a property for each item. It is useful for mapping an array of properties into a dictionary object.

  const props = [
    {name: 'name', value: 'size'},
    {name: 'type', value: 'string'},
    {name: 'descr', value: 'product size'}
  ];
  const nameValueListToObj = kvListToObj('name', 'value');
  nameValueListToObj(props)  //-> {name: 'size', type: 'string', descr: 'product size'}

makeMapFirstOfPairFn

makeMapFirstOfPairFn :: mapFn -> pair -> pair

This HOF takes a mapper and will create a function that, given a pair (i.e., an array of length two), will transform the first element of the pair. It is useful for mapping an array of pairs - often one created from an object's keys and values.

  const strings = ['hdr', 'My Heading'];
  const mapFirstToUpper = makeMapFirstOfPairFn(R.toUpper);
  mapFirstToUpper(strings)  //-> ['HDR', 'My Heading']

mapKeysOfObject

mapKeysOfObject :: mapFirstOfPairFn -> object -> object

This HOF takes a mapper and will create a function that will transform all keys of an object. The supplied mapper function must transform the first element of a pair array. Such a mapper can be made with the makeMapFirstOfPairFn HOF.

  const strings = {
    header: 'My Header',
    body: 'My Body',
    footer: 'My Footer'
  };
  const mapFirstToUpper = makeMapFirstOfPairFn(R.toUpper);
  const mapKeysToUpper = mapKeysOfObject(mapFirstToUpper);
  mapKeysToUpper(strings) //-> {HEADER: 'My Header', BODY: 'My Body', FOOTER: 'My Footer'}

pickNamesAndValues

pickNamesAndValues :: [object] -> object

Given an array of objects, each of which contains name and value properties, this function will return an object having the name values as keys, with associated values taken from the corresponding input value values.

const input = [
  {name: 'length', value: 10},
  {name: 'datatype', value: 'string'}
];
pickNamesAndValues(input)  //=> {length: 10, datatype: 'string'}

valueIsObject

valueIsObject :: a -> boolean
valueIsObject({foo: 'bar'})  //=> true
valueIsObject(42)  //=> false
valueIsObject([42, 43])  //=> false

valueNotObject

valueNotObject :: a -> boolean
valueNotObject(42)  //=> true
valueNotObject([42, 43])  //=> true
valueNotObject({foo: 'bar'})  //=> false

valueIsArray

valueIsArray :: a -> boolean
valueIsArray([])  //-> true
valueIsArray([1, 2, 3])  //-> true
valueIsArray({foo: 'bar'})  //-> false

isNotNil

isNotNil :: a -> boolean
isNotNil(42) //-> true
isNotNil({}) //-> true
isNotNil([]) //-> true
isNotNil(null) //-> false

isNotEquals

isNotEquals :: a -> b -> boolean
isNotEquals(42, 43) //-> true
isNotEquals(42, null) //-> true
isNotEquals(42, 21*2) //-> false
isNotEquals('foobar', 'foo'+'bar') //-> false

isoDateToMsec

isoDateToMsec :: isoString -> integer
  const date1 = new Date(Date.UTC(1970, 0, 1, 0, 0, 0));
  isoDateToMsec(date1.toISOString()) //-> 0
  const date2 = new Date('December 17, 1990 00:00:00');
  isoDateToMsec(date2) //-> 661420800000

dtToIsoLocal

dtToIsoLocal :: [Date || isoString] -> isoString
  const date1 = new Date('December 17, 2018 00:00:00');
  dtToIsoLocal(date1) //-> '2018-12-17T00:00:00-08:00'

sumProps

sumProps :: object -> number
const quarterSales = {q1: 100, q2: 90, q3: 120, q4: 130};
sumProps(quarterSales);  //=> 440
const noSales = {};
sumProps(noSales);  //=> 0

getKeyOfMaxProp

getKeyOfMaxProp :: object -> string
const quarterSales = {q1: 100, q2: 90, q3: 120, q4: 130};
getKeyOfMaxProp(quarterSales);  //=> 130
const noSales = {};
getKeyOfMaxProp(noSales);  //=> ''