GithubHelp home page GithubHelp logo

wellwelwel / jsonc.min Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 41 KB

✨ Faster and safer JSON and JSONC minify, parse and stringify for JavaScript (Browser compatible) — 2.62kB.

License: MIT License

TypeScript 100.00%
json jsonc minifier minify parse parser stringify jsonc-to-json dependency-free zero-dependency

jsonc.min's Introduction

jsonc.min

NPM Version GitHub Workflow Status (Node.js) GitHub Workflow Status (Bun) GitHub Workflow Status (Deno)

✨ Faster and safer JSON and JSONC minify, parse and stringify for JavaScript (Browser compatible) — 2.6kB.

Why

🔐 Safety

Many JSON minification packages rely on vulnerable regex, making them unsuitable for production.

    check jsonc.min prioritizes security by avoiding these pitfalls and offering a robust solution.

🤝 Compatibility

    check jsonc.min ensures full compatibility with both Node.js, Bun, Deno and, browser environments.
    check All features work for both JSON and JSONC.

🪶 Lightweight

    check Zero dependencies and optimized for production environments.


Install

Install Size

# Node.js
npm i jsonc.min
# Bun
bun add jsonc.min
# Deno
deno add npm:jsonc.min

Usage

Import

import { JSONC } from 'jsonc.min';
const { JSONC } = require('jsonc.min');

toJSON

Convert from JSONC to JSON.

JSONC.toJSON('/* JSONC content */ { "test": true }');
// "{ test: true }"

If the content is already JSON, it will just be preserved:

JSONC.toJSON('{ "test": true }');
// "{ test: true }"

minify

Minify both JSON and JSONC.

const content = `
  /**
   * JSONC content
   */
  {
    "test": true // 🔬
  }
`;

JSONC.minify(content);
// "{test:true}"
const content = `
  {
    "test": true
  }
`;

JSONC.minify(content);
// "{test:true}"

parse

Parse both JSON and JSONC.

const content = `
  /**
   * JSONC content
   */
  {
    "test": true // 🔬
  }
`;

JSONC.parse(content);
// { test: true }
const content = `
  {
    "test": true
  }
`;

JSONC.parse(content);
// { test: true }
  • If your content is guaranteed to be a JSON, there is no advantage to using JSONC.parse(content) instead of JSON.parse(content).

stringify

Prettify both JSON and JSONC.

Use JSON.stringify behind the scenes.

const content = '/** JSONC content */ { "test": true }';

JSONC.stringify(content, null, 2);
// "{
//    "test": true
//  }"
const content = '{ "test": true }';

JSONC.stringify(content, null, 2);
// "{
//    "test": true
//  }"

Examples

Reading a JSON or JSONC file

import { readFile } from 'node:fs/promises';
import { JSONC } from 'jsonc.min';

const content = await readFile('./file.jsonc', 'utf-8');

JSONC.parse(content);

Parsing a dynamic config file

For this example, let's assume a .configrc that can be both a JSON or a JSONC, as well as looking for both config.json and config.jsonc files:

import { JSONC } from 'jsonc.min';
import { cwd } from 'node:process';
import { join } from 'node:path';
import { readFile } from 'node:fs/promises';

export const getConfigs = async (customPath?: string) => {
  const targetRoot = cwd();

  const expectedFiles = customPath
    ? [customPath]
    : ['.configrc', 'config.json', 'config.jsonc'];

  for (const file of expectedFiles) {
    const filePath = join(targetRoot, file);

    try {
      const configsFile = await readFile(filePath, 'utf-8');

      // jsonc.min will parse both JSON and JSONC extensions, even if there is no extension.
      return JSONC.parse(configsFile);
    } catch {}

    return {};
  }
};

Acknowledgements

Contributors

jsonc.min's People

Contributors

wellwelwel avatar

Stargazers

João Palmeiro avatar Kevin Elliott avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.