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

blockman

v0.1.5

Published

Block-based array manipulation.

Downloads

6

Readme

blockman

Blockman is a block-based array manipulation tool that works similar to Array.map and Array.reduce, but for several elements at once. Written for image processing purposes.

$ npm install blockman
$ npm test

API

map = (data, f)

Like Array.map(), but iterates over blocks of data. The block size is inferred from the number of arguments supplied to the mapping function. The mapping function should always return an array.

var bm = require('blockman'),
    data = [1, 2, 3, 4, 5, 6];

bm.map(data, (x) => [x + 2]);
// [3, 4, 5, 6, 7, 8]

bm.map(data, (x, y) => [y, x]);
// [2, 1, 4, 3, 6, 5]

bm.map(data, (x, y, z) => [z, y, x]);
// [3, 2, 1, 6, 5, 4]

expand = (data, f)

Expand blocks of data. The block size is inferred from the number of arguments supplied to the expanding function. The expanding function should always return an array.

bm.expand([0], (x) => [x - 1, x, x + 1]);
// [-1, 0, 1]

bm.expand(data, (x, y) => [x, y, x + y]);
// [1, 2, 3, 3, 4, 7, 5, 6, 11]

bm.expand(data, (x, y, z) => [x, y, z, x * y * z]);
// [1, 2, 3, 6, 4, 5, 6, 120]

reduce = (data, f)

Reduce blocks of data. The block size is inferred from the number of arguments supplied to the reduction function. The reduction function should always return a Number.

bm.reduce(data, (x, y) => x + y);
// [3, 7, 11]

bm.reduce(data, (x, y, z) => x * y + z);
// [5, 26]

Usage

You can for example use it to easily reduce an RGBA image into a double precision grayscale bitmap array (each pixel a value from 0 to 1) and then expand it back into an RBGA array:

var fs = require('fs'),
    PNG = require('pngjs').PNG,
    bm = require('../blockman');

// 1. Read image into a node Buffer
var image = PNG.sync.read(fs.readFileSync('./test/test.png'));

// 2. Create a Float64Array view of the image data buffer
var f64 = new Float64Array(image.data);

// Reduce every 4 elements (R, G, B and alpha respectively).
// Resulting array will be 1/4 the size of the original.
var f64Gray = bm.reduce(f64,
  (r, g, b, a) =>
    0.2126 * (r / 255) +
    0.7152 * (g / 255) +
    0.0722 * (b / 255)
);

// 4. Expand the image back to its original size
f64Gray = bm.expand(
  f64Gray,
  x => [
    x * 255,
    x * 255,
    x * 255,
    255
  ]
);

// 5. Replace image data
image.data = new Uint8ClampedArray(f64Gray);

// 6. Write new image to file
fs.writeFileSync('./test/test.gray.png', PNG.sync.write(image));
console.log('wrote to ./test/test.gray.png');
console.log('---');