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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@micra/jsos

v0.0.6

Published

<p align="center"> <img src="https://raw.githubusercontent.com/micrajs/jsos/latest/.config/assets/logo.png" width="25%"> </p>

Downloads

25

Readme

About

This package includes a parser and transformer for this custom JS object schema structure.

Installation

yarn add -D jsos

Usage

jsosParser

The parser receives the JS object schema definition and creates an abstract tree.

Example

const definitions = {
  colors: {
    blue: {
      100: '#00f',
    },
  },
};

const elements = jsosParser(definitions);

The generated tree becomes:

[
  {
    "type": "NodeType",
    "path": "",
    "value": [
      {
        "type": "NodeType",
        "path": "colors",
        "value": [
          {
            "type": "NodeType",
            "path": "colors.blue",
            "value": [
              {
                "type": "StringType",
                "path": "colors.blue.100",
                "value": "#00f"
              }
            ]
          }
        ]
      }
    ]
  }
]

Elements types

interface JSOSParserNodeElement {
  type: 'NodeType';
  path: string;
  value: JSOSParserElement[];
}

interface JSOSParserStringElement {
  type: 'StringType';
  path: string;
  value: string;
}

interface JSOSParserNumericElement {
  type: 'NumericType';
  path: string;
  value: number;
}

interface JSOSParserListElement {
  type: 'ListType';
  path: string;
  value: JSOSParserElement[];
}

interface JSOSParserBooleanElement {
  type: 'BooleanType';
  path: string;
  value: boolean;
}

interface JSOSParserNullishElement {
  type: 'NullishType';
  path: string;
  value: null;
}

Other types:

interface JSOSParserRootElement {
  type: 'NodeType';
  path: '';
  value: JSOSParserElement[];
}

type JSOSParentElement =
  | JSOSParserNodeElement
  | JSOSParserListElement;

type JSOSPrimitiveElement =
  | JSOSParserStringElement
  | JSOSParserNumericElement
  | JSOSParserNullishElement
  | JSOSParserBooleanElement;

jsosTransformer

The transformer is responsible for navigating through the tree generated by the parser and transform it into something useful. It accepts a tree as the first argument and a set of options as the second.

To transform the tree, you can pass transformer functions to through the option object's transformers in the form of an object:

{
  transformers: {
    NodeType() { /* ... */ },
    StringType() { /* ... */ },
    NumericType() { /* ... */ },
    ListType() { /* ... */ },
    BooleanType() { /* ... */ },
    NullishType() { /* ... */ },
  }
}

Note: To define your transformers, you can import the NODE_TYPE, STRING_TYPE, NUMERIC_TYPE, LIST_TYPE, BOOLEAN_TYPE and NULLISH_TYPE variables and use them to define the keys.

Example

const elements = jsosParser(definitions);
const content = jsosTransformer(elements, {
  transformers: {
    [STRING_TYPE](element, { content, parseValue }) {
      content.append(
        `--${element.path.split('.').join('-').toLowerCase()}: ${parseValue(
          element.value,
        )};`,
      );
    },
  },
});

Results in:

--colors-blue-100: #00f;

jsosTransformer options

interface JSOSTransformerOptions<T = Record<string, any>> {
  valueParsers: Record<string, ValueParser>;
  transformers: Partial<
    Record<JSOSParserElementType, JSOSTransformerFunction<T>>
  >;
  makeContext?(context: JSOSTransformerContext): T;
  context: JSOSTransformerContext | (JSOSTransformerContext & T);
  initialValue: string | Record<string, any>;
}

Transformers' context

interface JSOSTransformerContext {
  elements: JSOSParserElement[];
  transform: JSOSTransformer;
  transformers: Record<JSOSParserElementType, JSOSTransformerFunction>;
  parseValue: ParseValue;
  content: Content;
  findByPath(
    path: string,
    elements?: JSOSParserElement[],
  ): JSOSParserElement | undefined;
}

The content is a class that contains the transpiled content. To modify the content you can use:

// If the initialValue is a string or undefined
interface StringContent {
  value: string;
  append(value: string): this;
  prepend(value: string): this;
}

// If the initialValue is an object
interface ObjectContent {
  value: Record<string, any>;
  append(path: string, value: any): this;
  prepend(path: string, value: any): this;
}

Test coverage

Author