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

@lab5e/data-mapper-chain

v0.2.1

Published

Simple data mapper library meant to be run in browser to ease data transformation for IoT devices in JS

Downloads

2

Readme

Data mapper chain

License Documentation npm bundle size (minified + gzip) data-mapper-chain CI

Simple data mapper library meant to be run in browser to ease data transformation for IoT devices in JS.

Example: Simple in browser

<body>
  ...
  <script src="https://cdn.jsdelivr.net/npm/@lab5e/data-mapper-chain"></script>
  <script>
    var myMapper = dmc
      .create()
      .chunk({ start: 2, size: 2 })
      .hexToInt();

    console.log(myMapper.mapData("babe")); // Prints 190
  </script>
</body>

Codepen example

Play around with this Codepen pen for a real life example usage of the data-mapper-chain.

Example: In ts

You must first install the dependency

npm i @lab5e/data-mapper-chain

Using shorthand

import { DataMapperChain } from "@lab5e/data-mapper-chain";

// Create a chain and add mappers
const dataMapperChain = new DataMapperChain()
  .chunk({
    start: 50,
    size: 4,
  })
  .hexToInt();

// Raw data from device
const deviceData: string = `47eee3803e3a8c713f8daf7242fc6666423c28c04111d84000024b00a3030c261b010b91d3`;

// Run mapper
dataMapperChain.mapData(deviceData); // prints 587

Instanciating mappers directly

import { DataMapperChain, Mappers } from "@lab5e/data-mapper-chain";

/**
 * We know that on byte 25 there is 2 bytes of data which is a hex encoded uint16
 * We solve this by doing the following:
 */

/**
 * Create a Chunk mapper
 */
const chunk = Mappers.chunk({
  start: 50,
  size: 4,
});

/**
 * Create a HexToInt mapper
 */
const hexToInt = Mappers.hexToInt();

// Create a DataMapperChain
const dataMapperChain = new DataMapperChain();

// Add mappers
dataMapperChain.addMapper(chunk);
dataMapperChain.addMapper(hexToInt);

// Raw data from device
const deviceData: string = `47eee3803e3a8c713f8daf7242fc6666423c28c04111d84000024b00a3030c261b010b91d3`;

// Run mapper
dataMapperChain.mapData(deviceData); // prints 587

Available mappers

All mappers have fully optional configurations, meaning if no configuration is provided it will fallback to sane defaults. It also supports partly providing parameters if you want to just override one option of the mapper.

Base64

Supports encoding and decoding of base64 input.

Configuration

Chunk

Take a chunk of the input and return it.

Configuration

FromJSON

Traverse a JSON struct and return value.

Configuration

HexToFloat

Take a hex input and convert it to a float.

Configuration

HexToInt

Take a hex input and convert it to an int.

Configuration

Offset

Take an input and offset it by a positive or negative value.

Configuration

History

What

The main workhorse is the DataMapperChain which serves a couple of purposes. It contains the different mappers you want to use in your "chain" of mappers and has functions to apply all mappers on a data set. It also allows for serializing configuration of both the chain and the added mappers. This serialized version can again be loaded directly into a new DataMapperChain which is now fully configured with the saved params.

Why

I found myself fiddling with a lot of IoT data recently and a need to graph it easily. The libs which which I found either relied heavily on eval or didn't have any typings. I put together this lib which is modular and pluggable and hopefully solves someones problem alongside mine.

Pluggable

While the lib provide a decent amount of mappers as a starting point, I know I don't cover every use case out there.

Tiny

The library relies mostly on native functions meaning it shouldn't get too big. More complex mappers should be application specific and be a part of the application which imports the library.

Development

We use TSDX for pretty much everything, and most npm scripts just proxy to tsdx.

Run single build

Use npm run build.

Run tests

To run tests, use npm test.

Configuration

Code quality is set up with prettier, husky, and lint-staged.

Jest

Jest tests are set up to run with npm test.

Watch mode

To run in watch mode run npm run test:watch

Coverage

To see coverage run npm run test:coverage

Bundle Analysis

size-limit is set up to calculate the real cost of your library with npm run size and visualize the bundle with npm run analyze.

Rollup

We us TSDX which uses Rollup as a bundler and generates multiple rollup configs for various module formats and build settings. See Optimizations for details.

We create UMD, CommonJS, and JavaScript Modules in our build. The appropriate paths are configured in package.json and dist/index.js

TypeScript

We use TypeScript for everything, giving us types for all the published packages.

Continuous Integration

GitHub Actions

  • main which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
  • size which comments cost comparison of your library on every pull request using size-limit

Publishing to NPM

We use np. To publish a new version, write npx np and follow the interactive tutorial.