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

openregister-picker-engine

v1.2.1

Published

An autocomplete engine for building openregister.org pickers.

Downloads

10,034

Readme

Openregister Picker Engine

Build Status

openregister-picker-engine is a JavaScript autocomplete engine. It's designed to interface an autocomplete widget (such as accessible-typeahead or corejs-typeahead) with data from the openregister.

Installation / Usage

As a node module

Install it using npm / yarn:

npm install --save openregister-picker-engine

Then use it:

import openregisterPickerEngine from 'openregister-picker-engine'

const suggest = openregisterPickerEngine({ url: '/public/data/location-picker-graph.json' })

API Documentation

openregisterPickerEngine(options)

options.url

Type: string

The path to the OpenRegister data file.

options.fallback

Type: function

An optional function that will be used as the suggest in the meantime until the graph loads or in the event that the graph fails to load.

options.callback

Type: function

An optional callback that will be executed when the graph has successfully finished loading and parsing.

options.additionalEntries

Type: array

An optional array to provide the engine with additional canonical entries. The array should look like this:

[
  { name: 'Atlantis', code: 'country:AN' },
  { name: 'Principality of Dorne', code: 'territory:DR' }
]

Where name is the searchable name of the entry and code is the primary key for the node.

options.additionalSynonyms

Type: array

An optional array to provide the engine with additional synonyms to use when searching for entries. The array should look like this:

[
  { name: 'Albion', code: 'country:GB' },
  { name: 'The Beautiful Country', code: 'country:IT' },
]

Where name is the searchable name of the synonym and code is the primary key for the node.

suggest(query, syncResults)

query

Type: string

The query to search for in the data file graph.

syncResults

Type: function

A function that will be called synchronously with the results from the provided query. The results will be an array of objects:

{
  name: string,
  path: string
}

The name is the canonical node of the graph node to display.

The path is an optional string specifying the last node that the engine had to pass through to reach the canonical node.

For example, if you seed the engine with the data for the Location Picker, and search for deut:

> const suggest = openregisterPickerEngine({ url: 'location-picker-graph.json' })
> suggest('deut', (results) => console.log(results))
[
  {
    name: 'Germany',
    path: 'Deutschland'
  }
]

This means that the location picker found 1 match for deut, which is Germany by way of its endonym Deutschland.

How it works

openregisterPickerEngine will perform a fetch request to get the graph data file.

The graph data file is a single JSON object with keys that match this schema:

{
  "country:GB": {
    "names": {
      "en-GB": "United Kingdom",
      "cy": "Y Deyrnas Unedig"
    },
    "meta": {
      "canonical": true,
      "canonical-mask": 1,
      "stable-name": true
    },
    "edges": {
      "from": []
    }
  }
}
  • country:GB is the primary key for this node.
  • names is an array that provides search strings in various locales. These are always displayable for canonical nodes but not necessarily for other nodes.
  • meta.canonical specifies if this is a canonical end node, which is something that the user is allowed to choose.
  • meta['canonical-mask'] is not used.
  • meta['stable-name'] specifies if this node's names are user displayable. (This is currently incorrect and will be replaced with a new property)
  • edges specifies any connections to other parts of the graph.
  • edges.from is an array of inbound nodes from this node, specified by their string primary keys.

This graph is turned into a reverse mapping of all of its name keys to their primary key. The keys of this reverse map are fed as seed data to a Bloodhound instance.

The openregisterPickerEngine function will return a suggest function. When this function is called with a query, that query is passed to the Bloodhound instance. This will search in all the available names and return an array of ones that match the query. These names are passed back into the reverse map to turn them into objects consisting of the matched name and the corresponding primary key.

These primary keys are then matched to their canonical primary keys by traversing the graph, and weighed based on their type of match and distance from the canonical node. The resulting objects are ordered, simplified, and sent back through the syncResults callback.

License

MIT.