Simplification at its finest
- Zero Non Dev Dependencies
- Syntactically Pleasing
- Extremely Lightweight
- Simply Enjoyable
- Minimalistic
- Simplified
- Tested
npm install --save-dev pickpocket.js
yarn add pickpocket.js --save
Simplified Wrapper Around Common Data Structure
Determine if there are "any" items in pocket
const pocket = pickpocket({
name: ['name is a required', 'name must be a string'],
email: ['email is a required field', 'email must be an email']
});
pocket.any();
true
Retrieve all data within pocket
const pocket = pickpocket({
name: ['name is a required', 'name must be a string'],
email: ['email is a required field', 'email must be an email']
});
pocket.all();
{
name: ['name is a required', 'name must be a string'],
email: ['email is a required field', 'email must be an email']
}
let data = { name: '', email: '' };
let rules = { name: 'required', email: 'email|required' };
let validation = validator(data, rules).validate();
validation.errors().all();
Flat list of all pocket field items
const pocket = pickpocket({
name: ['name is a required', 'name must be a string'],
email: ['email is a required field', 'email must be an email']
});
pocket.list();
[
'name is a required',
'name must be a string',
'email must be an email',
'email is a required field',
]
Set entirety of pocket data
const pocket = pickpocket({
name: ['name is a required', 'name must be a string'],
email: ['email is a required field', 'email must be an email']
});
pocket.set({
name: ['eyy'],
something: ['ohh']
});
Forget all pocket data
const pocket = pickpocket({
name: ['name is a required', 'name must be a string'],
email: ['email is a required field', 'email must be an email']
});
pocket.forget();
pocket.all(); // {}
Determine if specified field "has" items
const pocket = pickpocket({
tag: [],
name: [],
email: ['email is a required', 'email must be an email'],
});
pocket.has('tag'); // false
pocket.has('name'); // false
pocket.has('email'); // true
pocket.has('something_else'); // false
Get _first_ item from specific field
const pocket = pickpocket({
tag: [],
name: [],
email: ['email is a required', 'email must be an email'],
});
pocket.get('email');
List items for specific field
const pocket = pickpocket({
tag: [],
name: [],
email: ['email is a required', 'email must be an email'],
});
pocket.list('email');
['email is a required', 'email must be an email']
Add item to specific field
const pocket = pickpocket({
tag: [],
name: [],
email: ['email is a required', 'email must be an email'],
});
pocket.add('tag', 'simplified');
pocket.list('tag'); // ['simplified'];
pocket.add('email', 'extrafied');
pocket.list('email'); // ['email is a required', 'email must be an email', 'extrafied']
Set specified field items
const pocket = pickpocket({
tag: [],
name: [],
email: ['email is a required', 'email must be an email'],
});
pocket.set('email', ['set', 'email', 'items', 'list']);
pocket.list('email');
['set', 'email', 'items', 'list']
Forget items on the specified field
const pocket = pickpocket({
tag: [],
name: [],
email: ['email is a required', 'email must be an email'],
});
pocket.forget('email');
pocket.list('email');
[]
Pickpocket.js (Simplified)
Example One: Form Error Messages (Organized Per Field)
Common Example
const pickpocket = require('pickpocket.js');
const errors = pickpocket({
name: ['name is required', 'name is not a string', 'name needs to be cool'],
email: ['email field must be an email']
});
// errors.all()
// errors.list()
// errors.any()
// errors.add('name', 'message')
// errors.list('email')
// errors.has(field)
// errors.set({})
// errors.set('email', [])
// errors.forget()
// errors.forget('email')
// etc...
Example Two: Human Life Cycle (Organized By Life Moments: Life Moments Have Callback Functions)
Advanced example
const pickpocket = require('pickpocket.js');
const life = pickpocket({
before: [
firstBeforeMomentCallback,
secondBeforeMomentCallback,
thirdBeforeMomentCallback
],
passed: [
firstPassedMomentCallback,
secondPassedMomentCallback
],
failed: [
firstFailedMomentCallback
],
after: [
firstAfterMomentCallback,
secondAfterMomentCallback
],
});
life.eventful = (human = {}, moment) => {
if (life.has(moment)) {
this.list(moment).forEach(event => event(human));
life.forget(moment);
}
};
const moments = ['before', 'during', 'failed', 'passed', 'after'];
/**
* Add during life moment
*/
life.set('during', [
person => console.log(`${person.name} is going to kindergarden`),
person => console.log(`${person.name} was held back in kindergarden`),
person => {
person.age++;
console.log(`${person.name} had there birthday today`)
},
person => console.log(`${person.name} is ${person.age} and still in kindergarden`),
person => console.log(`${person.name} is going to have one heck of a time during life`),
// etc...
]);
let human = { age: 0, name: 'zak', birthed: 'April 22 1997' };
moments.forEach(moment => life.eventful(human, moment))
### Contribute
PRs are welcomed to this project.
If you want to improve the pickpocket.js library, add
functionality or improve the docs please feel free to submit a PR.
### License
MIT © [Zachary Horton ~ (Clean Code Studio)](https://github.com/zhorton34/pickpocket.js)