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

@streetcredlabs/categories

v3.3.2

Published

JS utility wrapper for places category taxonomy

Downloads

28

Readme

Categories Build Status

This is a JavaScript utility module that represents a category taxonomy (nested list) for places people care about in the world, aka Points of Interest (POIs). Categories represent the place type, for example a Starbucks is a coffee_shop.

Usage

Install via npm or yarn

yarn add @streetcredlabs/categories
or
npm i --save @streetcredlabs/categories

Import/require in the JavaScript client code

// using CommonJS
const { findById } = require('@streetcredlabs/categories');

// using ES6
import { findById } from '@streetcredlabs/categories';

API

The following top-level exports are available:

Languages

This TypeScript enum contains all available languages.

> const cats = require('./dist/bundle')
undefined
> `The following languages are available: ${Object.keys(cats.Languages).join(', ')}`
'The following languages are available: en, fil, id, ms, th, vi, zh-Hans'

findById

This function allows the client to look up the category object by its id. Categories may be nested.

import { findById } from '@streetcredlabs/categories';
const category = findById(1);
{
  "id": 1,
  "name": "Arts & Entertainment",
  "icon": "theatre",
  "categories": [ <category> ]
}

Call with Languages[lang] to get a certain category, with the name translated into the preferred language:

findById(9049, 'fil')

returns { id: 9049, name: 'Pasukan', icon: 'entrance', attributes: undefined }

dump

Parameters:

  • language (defaults to en): one of the available language codes found in the Languages enum
  • sortAlphabetically (defaults to true): whether or not to sort categories using a language-aware sorter

Returns the entire categories array.

import { dump } from "@streetcredlabs/categories";
const categories = dump();
[
  {
    "id": 1,
    "name": "Arts & Entertainment",
    "icon": "theatre",
    "categories": [...]
  },
  ...
]

Call with Languages[lang] as a second argument to get translated data: dump('fil')

getCompleteness

Determines how complete a place is based on our scoring system.

import { getCompleteness } from '@streetcredlabs/categories';
const place = funcThatGetsAStandardPlaceObj();
console.log(getCompleteness(place)); // 0.75

Contributing to this project

Pull requests are warmly welcomed.

Translations

This project uses lingui to generate gettext-format .po files containing all of the category names.

Adding a new language

As an example, here's how to add a translation for the language Ido, which uses the code io.

The first step to add a new translation is to use lingui to add a new locale and extract existing strings:

yarn lingui add-locale io
yarn lingui extract io

The first command will create a stub translation file in src/locales/io/messages.po and the second command will append all translatable strings into that file. Provide a translator with the messages.po, and have them fill in the msgstr ""s with translations of the preceeding msgid "..."s.

Once all of the translations are done, transform the po-file into the Lingui format:

yarn lingui compile --typescript

Include the completed translation in the library by adding it to src/index.ts:

diff --git a/src/index.ts b/src/index.ts
index e260261..7ad39f6 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -7,6 +7,7 @@ import ms from './locales/ms/messages';
 import th from './locales/th/messages';
 import vi from './locales/vi/messages';
 import zhHans from './locales/zh-Hans/messages';
+import io from './locales/io/messages';

 import categories from './data/categories';
 import getCompletenessUtil from './utils/getCompleteness';
@@ -20,6 +21,7 @@ const catalogs = {
   th,
   vi,
   'zh-Hans': zhHans,
+  io,
 };

 // Some shared types

Finally, run yarn build and give it a test run:

bc@bifurcaria:~/streetcred/categories$ node
Welcome to Node.js v12.6.0.
Type ".help" for more information.
> const cats = require('./dist/bundle')
undefined
> cats.findById(9001, 'io')
{ id: 9001, name: 'Aero', icon: 'airport', attributes: undefined }
>

Maintaining translations

The translations package script is a shortcut for extracting and compiling translations. It should be used after categories are added or modified, or if existing translations' po-files have been updated.

Local development and testing

Clone the repo

git clone [email protected]:streetcredlabs/categories.git && cd categories

Install dependencies

yarn

Run tests and watch for changes:

yarn test-watch

Run tests once:

yarn test

Run build and watch for changes:

yarn start

Run build once:

yarn build

Generate translation files

yarn translations

Releasing

Release a new version of this module based off the version in package.json. Note you will need to have publish access to the streetcredlabs npm account and be authenticated.

yarn release

The script does a few things:

  • Builds the dist version with yarn build (⚠️ NOTE: This needs improvement. Right now, if this results in changes, they won't be committed and it could be weird)
  • Creates a new git tag from the package.json version
  • Attempts to publish to npm with npm publish