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

file-it

v1.1.5

Published

Easily read/write/update JSON files

Downloads

121

Readme

file-it

Helps minimize the amount of fs read and write logic, try/catch logic, writes clean UTF8 json content, and cleans up byte order mark and newline characters to cleanly read and parse json content.

Installation

npm install --save file-it
OR
yarn add file-it

Import or Require

import fileIt from "file-it";
OR
const fileIt = require("file-it");

API


setJsonValue(filename, key, value, [options])

  • filename the full file path
  • key the name of the element in the json file
  • value the value you want to set
const fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.setJsonValue(file, "hello", "universe", {spaces: 2});

getJsonValue(filename, key)

  • filename the full file path
  • key the name of the element in the json file
const fileIt = require('file-it')
const file = '/tmp/data.json'
await fileIt.setJsonValue(file, "hello", "universe", {spaces: 2});
const val = await fileIt.getJsonValue(file, "hello");
console.log("val: ", val); // prints out "universe"

readJsonArraySync(filename)

  • filename the full file path
    • throws If JSON.parse throws an error, pass this error to the callback
const fileIt = require('file-it')
const file = '/tmp/jsonArrayFile.json'
fileIt.readJsonArraySync(file, function (err, data) {
  if (err) console.error(err)
  else console.log(data)
})

readJsonLinesSync(filename)

  • filename the full file path
    • throws If JSON.parse throws an error, pass this error to the callback
const fileIt = require('file-it')
const file = '/tmp/linesOfJsonData.json'
fileIt.readJsonLinesSync(file, function (err, data) {
  if (err) console.error(err)
  else console.log(data)
})

readContentFile(filename)

  • filename the full file path
const fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.readContentFile(file, function (err, data) {
  if (err) console.error(err)
  else console.log(data)
})

readContentFileSync(filename)

  • filename the full file path
const fileIt = require('file-it')
const file = '/tmp/data.json'
console.log(fileIt.readContentFileSync(file))

readJsonFile(filename)

  • filename the full file path
    • throws If JSON.parse throws an error, pass this error to the callback
const fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.readJsonFile(file, function (err, data) {
  if (err) console.error(err)
  else console.log(data)
})

You can also use this method with promises. The readJsonFile method will return a promise if you do not pass a callback function.

const fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.readJsonFile(file)
  .then(data => console.log(data))
  .catch(error => console.error(error))

readJsonFileSync(filename)

  • filename: the full file path
  • content: The string object to write
    • throws If an error is encountered reading or parsing the file, throw the error
const fileIt = require('file-it')
const file = '/tmp/data.json'

console.log(fileIt.readJsonFileSync(file))

appendJsonFileSync(filename, obj, [options])

  • filename: the full file path
  • obj: The json object to append to the file
  • options: Pass in any fs.appendFileSync options or set replacer for a JSON replacer. Can also pass in spaces and override EOL string.
const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.appendJsonFileSync(filename, content, function (err) {
  if (err) console.error(err)
})


writeContentFile(filename, content, callback)

  • filename: the full file path
  • content: The string object to write
const fileIt = require('file-it')

const file = '/tmp/data.txt'
const content = "hello world"

fileIt.writeContentFile(filename, content, function (err) {
  if (err) console.error(err)
})

writeContentFileSync(filename, content)

const fileIt = require('file-it')

const file = '/tmp/data.txt'
const content = "hello world"

fileIt.writeContentFile(filename, content)

writeJsonFile(filename, obj, [options], callback)

  • filename: the full file path
  • obj: The json object to write
  • options: Pass in any fs.writeFile options or set replacer for a JSON replacer. Can also pass in spaces and override EOL string.
const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFile(file, obj, function (err) {
  if (err) console.error(err)
})

Or use with promises as follows:

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFile(file, obj)
  .then(res => {
    console.log('Write complete')
  })
  .catch(error => console.error(error))

formatting with spaces:

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFile(file, obj, { spaces: 2 }, function (err) {
  if (err) console.error(err)
})

overriding EOL:

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
  if (err) console.error(err)
})

appending to an existing JSON file:

You can use fs.writeFile option { flag: 'a' } to achieve this.

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFile(file, obj, { flag: 'a' }, function (err) {
  if (err) console.error(err)
})

writeJsonFileSync(filename, obj, [options])

  • filename: the full file path
  • obj: The json object to write
  • options: Pass in any fs.writeFileSync options or set replacer for a JSON replacer. Can also pass in spaces and override EOL string.
const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFileSync(file, obj)

formatting with spaces:

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFileSync(file, obj, { spaces: 2 })

overriding EOL:

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFileSync(file, obj, { spaces: 2, EOL: '\r\n' })

appending to an existing JSON file:

You can use fs.writeFileSync option { flag: 'a' } to achieve this.

const fileIt = require('file-it')

const file = '/tmp/data.json'
const obj = { hello: 'World' }

fileIt.writeJsonFileSync(file, obj, { flag: 'a' })

findSortedJsonElement(filename, attribute, direction?)

  • filename: the full file path
  • attribute: the name of the attribute within a json element
  • direction: the sort direction ["asc" | "desc"] - default is desc
const fileIt = require('file-it')

const file = '/tmp/data.json'

const topElement = fileIt.findSortedJsonElement(file, "count")
const fileIt = require('file-it')

const file = '/tmp/data.json'

const bottomElement = fileIt.findSortedJsonElement(file, "count", "asc")