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

@beenotung/s-json

v0.1.2

Published

General purpose JSON viewer and editor

Downloads

10

Readme

s-json

General purpose data viewer and editor

npm Package Version

This package is renamed into s-data

Powered by S.js and surplus

Demo: https://s-json-demo.surge.sh/

Installation

npm install @beenotung/s-json

Typescript Signature

declare function JsonView(props: JsonViewProps<any>): HTMLElement

declare function JsonEdit(props: JsonEditProps<any>): HTMLElement | HTMLElement[]

type JsonViewProps<T> = {
  data: T,
  name?: string
  expandElement?: boolean,
}

type JsonEditProps<T> = {
  data: T,
  name?: string
  parent?: object
  updateValue?: (value: T) => void
  onChange?: (ev: Event, detail: UpdateDetail) => void
  editElement?: boolean,
}

type UpdateDetail = {
  parent?: object
  name?: string
  oldValue: any
  newValue: any,
}

Supported Data Type

  • HTMLElement
  • Text (DOM node)
  • string
  • number
  • timestamp (in number)
  • Date
  • bigint
  • boolean
  • function (show the source code)
  • symbol
  • undefined
  • null
  • Set
  • Map
  • Array
  • object

Usage Examples

This section demo using s-json with surplus, however it is not required.

JsonEdit, JsonView are javascript functions, that upon calling, return HTMLElement(s). Hence, it can be used with plain js as well.

Sample Data:

import * as Surplus from 'surplus';
Surplus;

export let sampleData = {
  username: 'Alice',
  age: 20,                  // render as input[type=number]
  todayTime: Date.now(),    // render as input[type=date] and input[type=time]
  todayDate: new Date(),
  salary: BigInt('12345'),  // render as input[type=text]
  isActive: true,
  sym: Symbol.for('alice'),
  partner: undefined,
  task: null,
  tagSet: new Set(['app', 'developer', 'web']),
  contactMap: new Map([
    [Symbol.for('bob'), {name: 'Bob', tel: '+85298765432'}],
    [Symbol.for('cherry'), {name: 'Cherry', tel: '+85223456789'}],
  ]),
  foodArr: ['seafood', 'potato'],
  profileObj: {
    name: 'Alice',
    age: 20,
  },
  // DOM elements
  friendNodes: [
    <span style={{fontSize: 'larger'}}>Bob</span>,
    <b>Cherry</b>,
    <a href='javascript:alert("clicked")'>Dave</a>,
    document.createTextNode('Eve'),
  ],
  // function
  onMessage: (msg: string) => alert('message: ' + msg),
}

Details refer to demo-data.tsx

Usage Example of JsonView

import * as Surplus from 'surplus';
import S from 's-js';
import { JsonView } from '../src/json-view';
import { sampleData } from './demo-data';
Surplus;

S.root(() => {
  // let main = JsonView({data: sampleData}) // without jsx
  let main = <JsonView data={sampleData} />; // with jsx
  document.body.appendChild(main);
});

Usage Example of JsonEdit:

import * as Surplus from 'surplus';
import S from 's-js';
import { JsonEdit, UpdateDetail } from '../src/json-edit';
import { sampleData } from './demo-data';
Surplus;

S.root(() => {
  let lastUpdate = S.data(Date.now());

  let main = (
    <div>
      Last update: {new Date(lastUpdate()).toLocaleString()}
      <h2>JsonEdit</h2>
      {JsonEdit({
        data: sampleData,
        editElement: true,
        onChange: (ev, { parent, name, newValue, oldValue }: UpdateDetail) => {
          console.log('change', { parent, name, newValue, oldValue });
          lastUpdate(Date.now());
        },
      })}
    </div>
  );
  document.body.appendChild(main);
});

Remark

JsonEdit updates the data in-place. Therefore, if you need to use JsonView (or other elements) to display the data editing by JsonEdit on the same page, you'll need to use a change signal in the container of JsonView. For example:

import * as Surplus from 'surplus';
import S from 's-js';
import { JsonEdit } from '../src/json-edit';
import { JsonView } from '../src/json-view';
import { sampleData } from './demo-data';

Surplus;

S.root(() => {
  let lastUpdate = S.data(Date.now());

  function ChangeDetect(props: { signal: any; children: any }) {
    return props.children;
  }

  let main = (
    <div>
      Last update: {new Date(lastUpdate()).toLocaleString()}
      <div style={{ display: 'flex' }}>
        <div>
          <h2>JsonView</h2>
          <ChangeDetect signal={lastUpdate()}>
            <JsonView data={sampleData} />
          </ChangeDetect>
        </div>
        <div>
          <h2>JsonEdit</h2>
          {JsonEdit({
            data: sampleData,
            editElement: true,
            onChange: (ev, detail) => {
              console.log('change', detail);
              lastUpdate(Date.now());
            },
          })}
        </div>
      </div>
    </div>
  );
  document.body.appendChild(main);
});

Details see demo.tsx