Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

structured-object

1.0.19 • Public • Published

structured-object

Assign dynamyc properties' names and data for object

Build Status

$ npm install structured-object

Assign fields' names and values for object properties. Then you can pass JSON object to .serialize() method and get object with replaced properties' names and data. Properties with value null will be replaced with data from fields.

var StructuredObject = require('structured-object').StructuredObject;
 
var struct = new StructuredObject();
 
struct.setField('firstField', 'FIRST_FIELD', {
    FIRST_FIELD_PROPERTY: 'FIRST_FIELD_VALUE'
});
struct.setField('secondField', 'SECOND_FIELD', 2);
 
struct.serialize({
    firstField: null,
    secondField: null
});
{
  "FIRST_FIELD": {
    "FIRST_FIELD_PROPERTY": "FIRST_FIELD_VALUE"
  },
  "SECOND_FIELD": 2
}

It's posiible to pass an object with some identical properties. All of them will be replaced. If value is not assigned, it remains unchanged in serialized object.

struct.serialize({
    firstField: {
        firstField: null,
        secondField: null,
        extraField: null
    },
    secondField: {
        extraField: {
            secondField: null
        }
    }
});
{
  "FIRST_FIELD": {
    "FIRST_FIELD": {
      "FIRST_FIELD_PROPERTY": "FIRST_FIELD_VALUE"
    },
    "SECOND_FIELD": 2,
    "extraField": null
  },
  "SECOND_FIELD": {
    "extraField": {
      "SECOND_FIELD": 2
    }
  }
}

It's able to get assigned fields.

struct.getField('secondField');
{
  "propertyName": "SECOND_FIELD",
  "data": 2
}

If there is no required field in the StructuredObject, .getField() returns an empty field with the same name.

struct.getField('extraField');
{
  "propertyName": "extraField",
  "data": null
}

Note!

If you call .serialize() method with the object which has the adjacent properties with the same StructuredObject field names, the serialized object will get the last enumerable one.

struct.setField('firstField', 'SECOND_FIELD', 1);
 
struct.serialize({
    firstField: null,
    secondField: null
});
{
  "SECOND_FIELD": 2
}

Install

npm i structured-object

DownloadsWeekly Downloads

2

Version

1.0.19

License

MIT

Last publish

Collaborators

  • avatar