1.1.0 • Published 5 years ago

rondel v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Created by: Alfredo Narváez, 2019

🛡️ Rondel

⚙️ Travis CI 🛠️

Build Status

👩‍🔬 SonarQube 👨‍🔬

Quality Gate Status Bugs Code Smells Coverage Lines of Code Maintainability Rating Reliability Rating Security Rating Vulnerabilities Technical Debt

Rondel is a library that makes use of the JavaScript Proxy API and exposes certain functionalities to protect & control objects through the use of handlers.

Why "Rondel"?

A rondel (ˈrɒndl) is a circular piece of metal used for protection in suits of armor

rondeljs

Installing

First things first! You gotta install rondel as a dependency, so go ahead and run:

yarn add -D rondel

or if you're using npm:

npm install --save-dev rondel

Okay! Once that's done, you're ready to go!

Protecting Objects:

Let's say you want to create a protected Object

import Rondel from 'rondel';

const rondel = new Rondel();

const myObj = rondel.create().protected({
  obj: { name: 'John', lastName: 'Doe' },
  modifiers: {},
});

console.log(myObj.randomProp); // unset property

How about controlling the undefined props of our objects?

import Rondel from 'rondel';

const rondel = new Rondel();

const myObj = rondel.create().protected({
  obj: { name: 'John', lastName: 'Doe' },
  modifiers: { exposeDefault: [] },
});

console.log(myObj.randomProp); // []

Alright... how about restricting setting properties?

import Rondel from 'rondel';

const rondel = new Rondel();

const myObj = rondel.create().protected({
  obj: { name: 'John', lastName: 'Doe' },
  modifiers: { exposeDefault: [], setNotAllowed: true },
});

console.log(myObj.randomProp); // []

myObj.addPropHere = 'Hello World!'; // Will throw error

Pseudo Type Validation

Rondel has a built-in type validation that can be triggered along with validateTypes: true on the modifiers object. We can then specify an array of the type of properties that we want to validate, so we end up getting something like this:

const myObj = rondel.create().protected({
  obj: { name: 'John', lastName: 'Doe' },
  modifiers: { validateTypes: true, areStrings: ['lastName', 'name'] },
});

if we then proceed to change name to a number like this:

myObj.name = 123;

A TypeError with the message Expected a string value for property name will be thrown.

Currently supported:

  • areStrings
  • areNumbers
  • areObjects

For more information please visit the Test file

Querying Objects by props

Proxies are very powerful. They also allow us to access dynamically generated properties.

Let's look at the following snippet:

const arr = rondel.create().searchable([
  {
    name: 'John',
    age: 30,
    skills: ['React', 'Node'],
    position: 'Sr Dev',
    salary: 100000,
  },
  { name: 'Mathew', age: 26, skills: ['JavaScript'], salary: 0 },
  {
    name: 'Claudia',
    nationality: null,
    age: 33,
    skills: ['AWS', 'Azure', 'DevOps', 'JavaScript'],
    salary: 80000,
  },
]);

By using findWhere<Property>Equals(<String>) we'll get all the matching results

arr.findWhereNameEquals('John'); // will give us all the objects that contain John in the name property

Other Examples

arr.findWhereNationalityIsNull(); // will give us all the objects that contain null in the nationality property
arr.findWhereSkillsIncludes('JavaScript'); // will give us all the objects that contain JavaScript inside an array of skills.

Currently supported methods:

findWhereXEquals

Returns an array of objects of all matching objects to value.

findWhereXEquals(value: any) : [{}];

findWhereXIsNull

Returns an array of objects of all matching null objects.

findWhereXIsNull() : [{}];

findWhereXIsUndefined

Returns an array of objects of all matching undefined objects.

findWhereXIsUndefined() : [{}];

findWhereXIsEmpty

Returns an array of objects of all matching Empty objects.

findWhereXIsEmpty() : [{}];

findWhereXIsIncludes

Returns an array of objects of all matching to the value/s provided.

findWhereXIsIncludes(value: any) : [{}];

findWhereXIsLowerThan & findWhereXIsGreaterThan

Returns an array of objects of all matching wether is lower or greater than a value provided.

findWhereXIsLowerThan(value: any) : [{}];
findWhereXIsGreaterThan(value: any) : [{}];
1.1.0

5 years ago

1.0.6

5 years ago