1.0.1 • Published 1 year ago

jsccon v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

JSON? Come on.. JSCON is more ✨ ENHANCED ✨

JSCON is a Node.js library for working with JSON and JSCON files. It provides methods for parsing, writing, and sorting files, as well as other cool features!

Installation

To install JSCON, use the following command, make sure it is spelled jsccon JSCCON NOT JSCON:

npm install jsccon

API

  • parse(file) - Read .jscon file and convert to JavaScript object, the file is an jscon file, should look like <filename>.jscon
  • sort(<file or url>, {by: "less|more", proprety: "<number property>", arrayProprety: "<array property>", output: "filename.jscon|object"}) Sort by property number or array values length where property is a number property and arrayProperty is an property that own array
  • merge(outputFileName, [files]) Merge multiple files into one file, where outputFileName is the output file name, and [files] is array of file names
  • set(file, newData) Set a new object to the file, newData is object
  • delete(file, {property: "property_name", value: "value"}) Delete an item from the file.
  • update(file, {property: "property_name", value: "value", newData: {age: "18", job: "Playing video games all night"}}) Update the data who has the same property and value with and add the newData.

Code sample:

Now. Let's say that's your json file:

[
  {
    "id": 1,
    "name": "Alice",
    "age": 25,
    "pets": [
      {
        "name": "Fluffy",
        "type": "cat"
      },
      {
        "name": "Rover",
        "type": "dog"
      }
    ]
  },
  {
    "id": 2,
    "name": "Bob",
    "age": 30,
    "pets": [
      {
        "name": "Spot",
        "type": "dog"
      }
    ]
  },
  {
    "id": 3,
    "name": "Charlie",
    "age": 20,
    "pets": []
  }
]

And that's how to use JSCON various functions to sort this JSON file:

Read & Transfer .JSCON file to JS object:

// Require the library 
const JSCON = require('jsccon');
const jscon = new JSCON();

// Parse a JSCON file
const data = jscon.parse('example.jscon');
console.log(data);

Sort a .JSCON file by less & more:

// Require the library 
const JSCON = require('jsccon');
const jscon = new JSCON();

// Sort a .JSCON file by less
jscon.sort('example.jscon', { by: 'less', property: 'age', output: 'sorted_data.jscon' });

// Sort a .JSCON file by more
jscon.sort('example.jscon', { by: 'more', property: 'age', output: 'sorted_data.jscon' });

// Sort a .JSCON file by less values in array
jscon.sort('example.jscon', { by: 'less', arrayProperty: 'pets', output: 'sorted_data.jscon' });

// Sort a .JSCON file by more values in array
jscon.sort('example.jscon', { by: 'more', arrayProperty: 'age', output: 'sorted_data.jscon' });

// Sort a .JSCON file by less, but return data as object
jscon.sort('example.jscon', { by: 'less', property: 'age', output: 'object' });

Merge multiple .JSCON files

// Require the library 
const JSCON = require('jsccon');
const jscon = new JSCON();

// Merge multiple JSCON files
const inputFiles = ['file1.jscon', 'file2.jscon'];
jscon.merge('merged_data.jscon', inputFiles);

Delete an item from a JSCON file:

// Require the library 
const JSCON = require('jsccon');
const jscon = new JSCON();

// Delete an item from a JSON file
jscon.delete('example.jscon', { property: 'name', value: 'Alice' });

Add an item to JSCON file

// Require the library 
const JSCON = require('jsccon');
const jscon = new JSCON();

// Add an item to a JSCON file
const newData = { name: 'Sarah', age: 28, pets: [] };
jscon.set('example.jscon', newData);

Update an item in a .JSCON file:

// Require the library 
const JSCON = require('jsccon');
const jscon = new JSCON();

// Update an item in a JSCON file
const updatedData = { age: 26 };
jscon.update('example.json', { property: 'name', value: 'Bob', newData: updatedData });

License

JSCON is released under the MIT License. Read more

Contribution:

Contributions are welcome! If you have any suggestions, feature requests, or bug reports, please open an issue on the GitHub repository, also be free to send important bugs to my email adress

Credits

Credits goes to abdlmutii, if you like this npm package please remember to check other work i've done on github and star this repo.