z-util

3.0.1 • Public • Published

pipeline status

Z-util

Z-util is a collection of libraries and pretty much anything else I feel like writing, it's mostly going to be small things such as simplifying things like interpreting environment info.

All of these libraries can be used by using:

const {DB, is, strore} = require('z-util')

Install

npm install --save z-util

zDB

zDB is essentially a small database that can export and import JSON.

Usage

Initialisation

zDB is a class and as such it needs to be initialised, it can be initialised with a settings object to change the behaviour.

const zDB = new DB({
  saveOnTransaction: true,
  importOnInit: false,
  importOnInitCallback: () => {},
  exportOverwrite: true,
  filePath: resolve('.', 'zDB.json')
})

The settings above are the defaults.

Settings

saveOnTransaction

This is a true/false value. If true then zDB.export will be called after an insert or delete.

importOnInit

This is a true/false value. If true then on initialisation it will look for a JSON file at 'filePath' and import it.

importOnInitCallback

This is a function. This is the callback that is passed to import after initialisation.

exportOverwrite

This it a true/false value. If true than an existing file at 'filePath' will be overwritten, it false then it will be read, and added to the data being exported.

filePath

This is the file that import will read and export will write. It can be changed later by:

zDB.settings.filePath = '/dbPath/zDB.json'

addTable

zDB.addTable('newTable')

Creates a table to be used as a 'where' parameter.

Insert

zDB.insert({
  key, value
}, where)
 
zDB.insert(key, value)

'where' is the table it will go into, if where is not defined, it goes into a table called 'default'.

Get

const data = zDB.get(key)
const data = zDB.get(key, where)

No explanation really needed, especially if you read insert.

Export

zDB.export()

Exports in JSON to zDB.settings.filePath. Returns a promise.

Import

zDB.import(cb)

Imports JSON from zDB.settings.filePath. Returns a promise.

is

zUtils.is({
  OS: 'Darwin',
  Arch: 'x64',
  Node: '9.2.0'
})

Checks process and returns an object, for example the above call when run on a Mac with Node version 8 would return:

{
  OS: true,
  Arch: true,
  Node: false
}

store

const localStore = new store.NewInstance() //Creates a local instance of store
store.globalStore.set('key', 'value') //Stores key/value in the global store

Store is what is behind zDB, it exports a class and a function, see above.

To get the stored value:

store.globalStore.get('key')

npm

Currently the npm part of this module can only search and requires an API key. You can get the API key from https://libraries.io/api.

search

npm.search('z-util', apiKey) //apiKey only needs to be set once as it is cached

isConnected

I wrote isConnected so that I could run my tests on the bus without getting errors from the npm test. It simply sends an http request to google, if the status code is 200 or 302 it resolves, otherwise it rejects. It returns a promise.

isConnected()
  .then(statusCode => {})
  .catch(err => {})

array

I wrote array because I was bored and thought it could be useful.

contains

if (array.contains(arr, valueToCheckFor)) {}

Returns true if the value is found as an element in the array, currently does not search below the first level of elements. Returns false if it doesn't return true.

flatten

const toFlatten = [
  'hi',
  {key, 'value'},
  ['hi', 'again']
]
 
const flattenedNoObjs = flatten(toFlatten)
flattenedNoObjs === [
  'hi',
  {key, 'value'},
  'hi',
  'again'
]
 
const flattenedObjs = flatten(toFlatten, true)
flattenedObjs === [
  'hi',
  'value',
  'hi',
  'again'
]

This flattens arrays and can extract the values out of objects as well. Goes as many levels deep as node's memory stack allows.

Package Sidebar

Install

npm i z-util

Weekly Downloads

6

Version

3.0.1

License

MIT

Last publish

Collaborators

  • imzacm