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

@jsmanifest/content-combiner

v1.0.9

Published

Combine content from multiple sources and transform them into one unified data structure to work with

Downloads

4

Readme

@jsmanifest/content-combiner

Grab data from multiple sources by providing a fetcher and a keymapping that you provide for each source which will translate their data structures to a unified one that you define by using the Keymapper API from this library.

Installation

npm install --save @jsmanifest/content-combiner

Usage

Set up a fetcher for data and assign a keymapper to transform its data objects:

import api from '../../reporter-api'
import Keymapper from '../Keymapper'
import ContentCombiner from '../Combiner'

const mockData2 = [
  {
    title: 'ARSON AT HALL OF JUSTICE',
    details:
      "<p>Seeking the public's assistance in identifying the individuals who participated in an arson that occurred at the Louisville Hall of Justice,</p>",
    dates_of_birth_used: ['January 21st, 2001', 'January 22nd, 2001'],
    description: 'Louisville, Kentucky\r\nSeptember 23, 2020',
    url: 'https://www.hojustice.com/at-hall-of-justice',
    uid: '061f36e9ed009amk387988c38459dd2',
    sex: null,
    modified: '2020-09-25T14:00:55+00:00',
    published: '2020-09-25T14:00:55+00:00',
    relatives: {
      sisters: ['Jeanie Mo', 'Jane Nurse'],
      brothers: ['Michael Mo', 'Solomon Mo', { firstName: 'Bennie', lastName: 'Mo' }],
    },
  },
  {
    title: 'HOMICIDE AT QUEENSVILLE',
    details: 'last seen walking at a park in queensland',
    description: 'Louisville, Kentucky\r\nSeptember 23, 2020',
    url: 'https://cardiac-homeland.com/homicide-at-queensville/',
    uid: '061f36e9a3d9w387988c38459dd2',
    sex: null,
    modified: '2020-09-28T14:00:55+00:00',
    published: '2020-09-26T14:00:55+00:00',
    relatives: null,
  },
]

const combiner = new ContentCombiner()

const getSiblings = (type) => (item) => {
  if (item && item[type]) {
    return {
      [type]: item[type].map((fullName) => {
        if (typeof fullName === 'string') {
          const [firstName, lastName] = fullName.split(' ')
          return {
            firstName,
            lastName,
          }
        } else if (fullName && typeof fullName === 'object') {
          return {
            firstName: fullName.firstName,
            lastName: fullName.lastName,
          }
        }
      }),
    }
  }
}

const getBrothers = getSiblings('brothers')
const getSisters = getSiblings('sisters')

const composeSiblingMappers = (...fns) => {
  return (item) =>
    fns.reduceRight(
      (acc, fn) => (acc ? Object.assign(acc, fn(item)) : fn(item) || acc),
      undefined,
    )
}

// prettier-ignore
combiner#createFetcher(() => Promise.resolve(mockData2), {
  keymappers: {
    subtitle: 'description',
    description: 'details',
    updatedAt: 'modified',
    siblings: (item) => composeSiblingMappers(
      getBrothers,
      getSisters
    )(get(item, 'relatives')),
  },
})

combiner
  .execute()
  .then((results) => {
    console.log(JSON.stringify(results, null, 2))
  })
  .catch(console.error)

Result:

[
  {
    "subtitle": "Louisville, Kentucky\r\nSeptember 23, 2020",
    "description": "<p>Seeking the public's assistance in identifying the individuals who participated in an arson that occurred at the Louisville Hall of Justice,</p>",
    "updatedAt": "2020-09-25T14:00:55+00:00",
    "siblings": {
      "sisters": [
        {
          "firstName": "Jeanie",
          "lastName": "Mo"
        },
        {
          "firstName": "Jane",
          "lastName": "Nurse"
        }
      ],
      "brothers": [
        {
          "firstName": "Michael",
          "lastName": "Mo"
        },
        {
          "firstName": "Solomon",
          "lastName": "Mo"
        },
        {
          "firstName": "Bennie",
          "lastName": "Mo"
        }
      ]
    }
  },
  {
    "subtitle": "Louisville, Kentucky\r\nSeptember 23, 2020",
    "description": "last seen walking at a park in queensland",
    "updatedAt": "2020-09-28T14:00:55+00:00"
  }
]

Combining multiple fetchers from different sources/apis to return the same data structure

Apply the previous steps starting with createFetcher. Once execute is called the fetcher will be called and each returned result of each fetcher will be used to apply the keymappings. If the result you return from the fetch function is an array, it will map over each item and return an array of the mapped results. If the fetch function returns an object, it will return a single object with the mapped result.

When you have lets say 10 different fetchers and call execute, you will receive an array of 10 items (each of which will be an array or the single object from the corresponding fetcher)

You have now a unified data structure and what you want to do with that data is all up to you.

API

Combiner

Combiner#execute

`Combiner#createFetcher

Combiner#getKeymap

Combiner#getKeymapper

Combiner#setKeymapper

Combiner#getDataKeys

Combiner#setDataKeys

Keymapper

Keymapper#get

Keymapper#set

Keymapper#parse