object-roomba

clean your dirty objects

Usage no npm install needed!

<script type="module">
  import objectRoomba from 'https://cdn.skypack.dev/object-roomba';
</script>

README

object-roomba Build Status

Clean your dirty objects

js-standard-style

Userful when needing to parse objects from source that doesn't support data types declerations like csv files etc.

Usage

var roomba = require('object-roomba')

var schema = {
  age: Number,
  expiration: Date,
  is_active: Boolean,
  name: String,
  address: function (input) {
    if (!input) { return null }
    return input
  }
}

var clean = roomba(schema)

var dirty_obj = {
  age: '18',
  expiration: '2015-08-17 19:30:30.219',
  is_active: 'false',
  name: 'Michael D',
  address: ''
}

clean_obj = clean(dirty_obj)
console.log(clean_obj)
// {
//   age: 18,
//   expiration: Mon Aug 17 2015 19:30:30 GMT+0300 (IDT),
//   is_active: false,
//   name: 'Michael D',
//   address: null
// }

Currently supporting

  • String
  • Number
  • Date
  • Boolean

Options

You can pass options to roomba:

var clean = roomba(opts, schema)

Available options are:

  • remove_extra_fields - By default, fields that are not in the schema are kept untouched, you can remove them from the cleaned obj by setting this to true.