@nw.lee/safe-json
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Safe-Json

The functions that help to parse JSON objects and array safely.

Array

filterArray

Filter items from array that have properties(keys) specified in the parameter includes, and don't have in the parameter excludes.

The parameters, includes and excludes, is set to empty array at default.

const filtered = filterArray(array, ["id", "name", "createdAt"], ["unused_keys"])

refineArray

Return another array that only includes properties specified in the parameters.

const refined = refineArray(
  array,
  ["ID", "Name", "created_AT", "COUNTS", "MUST_haveValue"],
  ["id", "name", "createdAt", "counts", "mustHaveValue"]
)

Object

accessObject

Return the corresponding value if the property is in object, or null.

const temp = {
  id: 1,
  name: "Temporary Object",
  createdAt: "20210301"
  counts: 30,
}
const value = accessObject(temp, "counts")
// value === 30

refineObject

Return new object by changing the keys in the object.

const refined = refineObject(temp, ["id", "name", "createdAt", "counts", "category", "tags"])

Package Sidebar

Install

npm i @nw.lee/safe-json

Weekly Downloads

1

Version

0.1.0

License

ISC

Unpacked Size

30.3 kB

Total Files

13

Last publish

Collaborators

  • nw.lee