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

@google-automations/object-selector

v5.0.0

Published

Utilities for selecting objects

Downloads

52

Readme

object-selector

This is a small library for selecting objects. The main user of this library are the bot authors who want to choose objects based on their propery values and make it configurable in the bot config files.

  1. Bot author define the config file.

"I want the bot user or org admins can configure some conditions for the target objects."

  1. Bot user or org admins create the config file with some selector definitions.

    "I want the bot to skip private repositories" ["private", "eq", "false"]

  2. The bot can choose the intended objects.

    "Ok, I skip private repositories"

Background

Here is an example attempt to implement something similar: https://github.com/googleapis/.github/pull/9/files

The first user of this library will be the sync-repo-settings bot for implementing its org level config(#1884).

Install

npm i @google-automations/object-selector

Concepts

This library allows bot authors to have rules for selecting objects in its config file. The smallest config element is called selector.

type SelectorValueType = string | number | boolean | Array<string>;

export type Operator = 'eq' | 'ne' | 'anyof' | 'regex';

export type Selector = [string, Operator, SelectorValueType];

In the yaml config file, it looks like:

["name", "regex", "(nodejs|javascript|typescript)"]

or

["private", "eq", false]

Then we call a list of selectors Selectors.

export type Selectors = Array<Selector>;

In the yaml config:

- ["private", "eq", false]
- ["archived", "eq", false]
- ["org", "anyof", ["GoogleCloudPlatform", "googleapis"]]
- ["name", "regex", "(nodejs|javascript|typescript)"]

A single Selectors represents a condition where all the selectors are combined with AND.

Usage

It is strongly recommended to use the provided schema file for schema validation of your config file. The schema is defined in selectors-schema.json file and you can refer the definition of Selectors as @google-automations/selectors/v1 and Selector as @google-automations/selector/v1.

You will likely have to provide the schema file to the validator:

import schema from './my-schema.json';
import selectorsSchema
  from '@google-automations/object-selector/build/src/selectors-schema.json';

const checker = new ConfigChecker<Config>(
  schema, CONFIG_FILE_NAME, [selectorsSchema]
);

Then you can refer the definition from your schema.

{ "$ref": "@google-automations/selectors/v1" }
{ "$ref": "@google-automations/selector/v1" }

You can also import the types from this library.

import {
  SelectorValueType,
  Selector,
  Selectors
} from '@google-automations/object-selector';

Select the object

Let's say you want to filter the Github repositories. The code example for selecting archived repositories goes like this:

import {Endpoints} from '@octokit/types';

import {
  Selectors,
  ObjectSelector,
  RepoDescriptorConvertor
} from '@google-automations/object-selector';

export type Repository =
  Endpoints['GET /repos/{owner}/{repo}']['response']['data'];

const selectors:Selectors = [["archived", "eq", true]];

const repos = await(getRepos()); // Somehow you got all the repos.

// It accepts a list of `Selectors`(list of list of selectors).
// Each `Selectors` are combined by `OR`.
const selector = new ObjectSelector<Repository>(
  [selectors], RepoDescriptorConvertor
)
const selected = selector.select(repos);
// Now `selected` contains archived repos.
// or you can call `match` with an individual object for boolean result.
for (const repo of repos) {
  if (selector.match(repo)) {
    // do something
  }
}

CLI and recipes for repositories

The library provides cli, which is a small demo application for selecting repositories. You can install the cli by:

$ cd packages/object-selector
$ npm link .

You can try filtering some repositories, for example, you can list nodejs repositories form a small data file by:

$ object-selector test-yaml -f test/fixtures/repos.json -y recipes/nodejs.yaml

It will accept multiple recipe files in recipes directory:

$ object-selector test-yaml -f test/fixtures/repos.json -y recipes/*.yaml

If the data file test/fixtures/repos.json is not enough for you, you can dump all the repos with dump command. For dumping the real data, you have to set GITHUB_TOKEN environment variable:

$ export GITHUB_TOKEN=$(cat your-github-token-file)
$ object-selector dump

This will create repositories-dump.json for bigger dataset. The test-yaml command will use this file by default.

$ object-selector test-yaml -y recipes/*.yaml