|
1 | | -getinto |
| 1 | +# getinto |
| 2 | + |
| 3 | +getinto is a simple package to deep get something in an object. |
| 4 | +It always return a value (array, function, string, object...) or undefined. Never a error (if you use the correct sintax). |
| 5 | +Say bye for error "Cannot read property of undefined" |
| 6 | + - Simple sintax |
| 7 | + - TypeScript compatibility |
| 8 | + - Can be use with object chain and function chain mixed |
| 9 | + |
| 10 | + |
| 11 | +### Quick Example |
| 12 | +```js |
| 13 | +const into = require('getinto') |
| 14 | +const animals = { |
| 15 | + pets: [ |
| 16 | + { |
| 17 | + name: 'Rex', |
| 18 | + owner: { |
| 19 | + name: 'Jon', |
| 20 | + address: { |
| 21 | + country: 'USA', |
| 22 | + province: 'British Columbia' |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + ] |
| 27 | +} |
| 28 | + |
| 29 | +const province = into(animals) |
| 30 | + .into('pets') |
| 31 | + .into('1') |
| 32 | + .into('owner') |
| 33 | + .into('adress') |
| 34 | + .get('province') //get return a array, function, string, object... or undefined. Never a error. |
| 35 | +console.log(province) //log: 'British Columbia' |
| 36 | + |
| 37 | +const errorPetName = animals.notExist.name //errror: Cannot read property 'name' of undefined |
| 38 | +const petName = into(animals) |
| 39 | + .into('notExist') |
| 40 | + .get('name') |
| 41 | +console.log(petName) //log: undefined |
| 42 | +``` |
| 43 | + |
| 44 | +### Installation |
| 45 | +```sh |
| 46 | +$ npm install getinto |
| 47 | +or |
| 48 | +$ yarn add getinto |
| 49 | +``` |
| 50 | + |
| 51 | +### Usage |
| 52 | +I suggest the following way to import |
| 53 | +```js |
| 54 | +const into = require('getinto') |
| 55 | +``` |
| 56 | +You can write a chan of 'into'. The first into comes from import and receive an object (or a function, see in 'More examples' or in 'API' for details). The next intos come from previous into. The latter statement must be 'get'. |
| 57 | +```js |
| 58 | +const into = require('getinto') |
| 59 | +const value = into(object) |
| 60 | + .into('objectPropertie') |
| 61 | + .into('nextObjectPropertie') |
| 62 | + ... |
| 63 | + ... |
| 64 | + .into('otherPropertie') |
| 65 | + .get('returnedPropertie') |
| 66 | +console.log(value) //log: returnedPropertie |
| 67 | +``` |
| 68 | +### Typescript Usage |
| 69 | +Writing |
| 70 | + |
| 71 | +### API |
| 72 | +writing the compleate API documentation |
| 73 | + |
| 74 | +### More Examples |
| 75 | +```js |
| 76 | +const into = require('getinto') |
| 77 | +const business = { |
| 78 | + stories: [ |
| 79 | + { |
| 80 | + companyName: 'megaPet, INC', |
| 81 | + products: ['dogFood', 'dogBed'], |
| 82 | + stack: ['react', 'node.js', 'mongoDB'], |
| 83 | + doSomething: function (name) {console.log(name + ' says Hello World')} |
| 84 | + }, |
| 85 | + { |
| 86 | + companyName: function (baseName) { return baseName + ', INC' }, |
| 87 | + products: function (a, b) { return [a + 'Product', b + 'Product']}, |
| 88 | + doSomething: function () {console.log('Hello World')} |
| 89 | + } |
| 90 | + ] |
| 91 | +} |
| 92 | + |
| 93 | +business[0].stories.notExist.techs[0] //error: Cannot read property 'techs' of undefined |
| 94 | +into(business) |
| 95 | + .into('0') |
| 96 | + .into('notExist') |
| 97 | + .into('techs') |
| 98 | + .get('0') //undefined |
| 99 | + |
| 100 | +into(business) |
| 101 | + .into('stories') |
| 102 | + .into('0') |
| 103 | + .get('companyName') // 'megaPet, INC' - if something don't exist return undefined |
| 104 | + |
| 105 | +into(business) |
| 106 | + .into('stories') |
| 107 | + .into('0') |
| 108 | + .get('products') // ['dogFood', 'dogBed'] - if something don't exist return undefined |
| 109 | + |
| 110 | +into(business) |
| 111 | + .into('stories') |
| 112 | + .into('0') |
| 113 | + .into('stack') |
| 114 | + .get('1') // 'node.js' - if something don't exist return undefined |
| 115 | + |
| 116 | +const functionExample = into(business) |
| 117 | + .into('stories') |
| 118 | + .into('0') |
| 119 | + .get('doSomething') |
| 120 | + functionExample('Jonathan') //log: 'Jonathan says Hello World' - if something don't exist return undefined |
| 121 | + |
| 122 | +into(business) |
| 123 | + .into('stories') |
| 124 | + .into('1') |
| 125 | + .get('companyName', 'superMarket') // 'superMarket, INC' - executed like companyName('superMarket') |
| 126 | + |
| 127 | +const productA = 'fishmonger' |
| 128 | +into(business) |
| 129 | + .into('stories') |
| 130 | + .into('1') |
| 131 | + .get('products', [productA, 'bakery']) // ['fishmongerProducts', 'bakeryProducts'] - executed like products(productA, 'bakery') |
| 132 | + |
| 133 | +into(business) |
| 134 | + .into('stories') |
| 135 | + .into('1') |
| 136 | + .get('doSomething', []) //log: 'Hello World - executed like doSomething() |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +### array example |
| 141 | +Writing |
| 142 | +### object example |
| 143 | +Writing |
| 144 | +### function example |
| 145 | +Writing |
| 146 | + |
| 147 | +### Todos |
| 148 | +Writing |
| 149 | + |
0 commit comments