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

k2

v0.14.2

Published

Functional javascript utils

Downloads

22

Readme

K2

Functional javascript utilities

NPM

manpm Circle CI Code Climate

Install

npm install --save k2

In the browser, if you already have global lodash library, you can include dist/k2-browser.js. Under Node, or to use lodash included in the k2 bundle, use dist/k2.js.

// node
var k2 = require('k2');

Api

presentProperties

Determines if each property in the list is present in each object given in the second list

var bad = [{
  name: 'foo',
  age: 1
}, {
  name: 'bar'
}, {
  name: 'baz'
}];
presentProperties(['name', 'age'], bad);
// ['name']

presentProperties is curried, works with deep properties, requires lodash version with deep property support, see _.has

cleanEnteredText

Removes HTML entities from user-entered text. Common entities introduced by a textarea element are   at the end.

var cleanText = k2.cleanEnteredText(text);
// text - string entered into a textarea
k2.cleanEnteredText('FOO b') // 'foo b'
k2.cleanEnteredText('foo ') // 'foo'

In addition to cleanEnteredText, there is also cleanHtmlTags and cleanTickerSearchHtml

findPartialMatches

Finds all items where given properties are matching the given query text.

var matches = k2.findPartialMatches(properties, items, queryText);
// properties - single string or an array of strings
// items - objects to search over
// queryText - single string
// find all items where property 'foo' contains 'ooo'
var items = [{
  foo: 'foo'
}, {
  foo: 'foo2'
}, {
  foo: 'bar'
}];
var result = k2.findPartialMatches('foo', items, 'oo');
// result is [ items[0], items[1] ]

rankPartialMatches

Useful to order items by text entered by the user, but only considering matches of query text to certain properties. Items without any matches will be pushed to the back of the returned list.

var matches = k2.rankPartialMatches(properties, matches, queryText);
// properties - single string or array of strings
// matches - objects to search
// queryText - single string
var items = [{
    foo: 'foo'
}, {
    foo: 'foo2',
    bar: 'abar'
}, {
    foo: 'bar'
}];
var result = k2.rankPartialMatches(['foo', 'bar'], items, 'bar');
// result is a new array with 3 items
// result[0] is items[2] (exact match)
// result[1] is items[1] (partial match)
// result[2] is items[0] (nothing matches)

objectLens

A function for building immutable object lenses. Construct the lens with a key and then use its getter, setter, or mapper.

objectLens('name')({ name: 'joe' });
// => 'joe'
objectLens('age').set(20, { age: 19 });
// => { age: 20 }
objectLens('age').map(R.add(1), { age: 19 });
// => { age: 20 }

To compose setters, use function composition!

var setter = R.compose(objectLens('name').set('matt'),
  objectLens('age').map(R.add(1)));
setter({ name: 'joe', age: 19 })
// => { name: 'matt', age: 20 }

onlyTrue

XOR for predicates - returns true if and only if a single value is truthy.

onlyTrue(true, false, false); // true
onlyTrue(false, false, false); // false
onlyTrue(false, true, true); // false
onlyTrue(false, false, true); // true

guessDateFormat

Tries to determine date format from single or list of strings. If there is ambiguity returns undefined.

guessDateFormat('2010-15-10'); // 'YYYY-DD-MM'
guessDateFormat(['05/19/2000', '22/01/2002']); // undefined

fanout

Apply a collection of functions to the same input and collect their results in a list.

fanout(prop(0), prop(1))(['a', 'b']) == ['a', 'b']
fanout(length)(['a', 'b']) == [2]

Small print

Author: Kensho © 2015

Support: if you find any problems with this library, open issue on Github

MIT License

The MIT License (MIT)

Copyright (c) 2015 Kensho

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.