New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fnk

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fnk

buncha functions and stuff

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

A library of utility functions, written in ES6 and transpiled using babel and uglify.

Taken from a handful of utility functions I found myself using all over the place in my fork of RanvierMUD.

Install using npm install fnk --save

Use cases:

const _ = require('fnk');

// Finding something in a string or array:
_.has('potatoes', 'toes')   // true
_.has([true, false], false) // true
_.hasNot([true, false], 'toes') // true


// Finding the difference between two arrays.
_.arrayDiff(['a', 'b', 'c'], ['a']) // ['b', 'c']


// Memoization of functions, using ES6 Maps. 
// Fn can take variable arguments.
// Polyfill if Maps are not supported natively.
_.cache(fn);  
fn(x) // does something complicated
fn(x) // simply gets result from cache


// Create and fill an array with a value.
// Equivalent to new Array(n).fill(v), but does not require Array.fill polyfill.
_.fillArray(4, 'bazinga') // ['bazinga', 'bazinga', 'bazinga', 'bazinga']


// Creating a functional iterator.
// Will call the function N times, passing in the result each time.
const quadruple = _.createIterator(x => x + x, 2);
quadruple('f') // 'ffff'


// Get values from an object
// kind of a polyfill for Object.values
// but shortcuts if the object already has a .values method.
_.values({1: 'a', 2: 'b'}) // ['a', 'b']


// Reduce over values, shortcut for _.values(obj).reduce
_.reduceValues({1: 'a', 2: 'b'}, (x, acc) => acc + x, '') // 'ab'


// Checks to see if the object is empty or not...
_.hasKeys({}) // false
_.haskeys({'potato': undefined}) // true


// Function composition with variable number of functions.
// Reduces functions right to left.
// Only takes a single arg so it's best to pass in an object and destructure it in each of the functions.
const doStupidMath = _.compose(x => x + 1, x => x * 2, x => x + 100);
doStupidMath(2) // 205


// Pads leftly
_.leftPad(10, 'potatoes', '*') // '**potatoes'
_.leftPad(5, 'potatos', '*') // 'potatoes'

// Nicer-looking typechecking
const isPlayer = (thing) => _.is(Player, thing);
isPlayer(potato) // false
isPlayer(new Player()) // true

// Setting bounds for numbers to ensure they are in a certain range.
const mustBePositive = _.setBounds(1, Infinity);
mustBePositive(-100) // 1
mustBePositive(52) // 52

Keywords

FAQs

Package last updated on 30 Jan 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc