@alfdocimo/rondel

1.0.5 • Public • Published

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.createProtected({
  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.createProtected({
  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.createProtected({
  obj: { name: 'John', lastName: 'Doe' },
  modifiers: { exposeDefault: [], setNotAllowed: true },
});

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

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

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.getProtected([
  {
    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 object that contains John

Other Examples

arr.findWhereNationalityIsNull()); // will give us all the object that contains Claudia
arr.findWhereSkillsIncludes('JavaScript')); // will give us all the object that contains Mathew

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) : [{}];

Readme

Keywords

Package Sidebar

Install

npm i @alfdocimo/rondel

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

285 kB

Total Files

13

Last publish

Collaborators

  • alfdocimo