Skip to content

Commit 2ae21ac

Browse files
committed
Merge branch 'develop' into main
2 parents ef03377 + cdb1667 commit 2ae21ac

16 files changed

Lines changed: 4096 additions & 164 deletions

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
node_modules/
2-
teste.ts
1+
node_modules/

.npmignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 116 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,84 @@
11
# getinto
22

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"
3+
getinto is a simple package to deep get something in an object, array and function.
4+
If not exist, return undefined. Never a error.
5+
Say bye for the error "Cannot read property of undefined".
66
- Simple sintax
7-
- TypeScript compatibility
8-
- Can be use with object chain and function chain mixed
7+
- Javascript and TypeScript compatibility
8+
- Can be used with object chain, function chain and array chain mixed
99

1010

11-
### Quick Example
11+
## Quick Example
1212
```js
1313
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-
}
14+
const pets: [
15+
{
16+
name: 'Rex',
17+
owner: {
18+
name: 'Jon',
19+
address: {
20+
country: 'Canada',
21+
province: 'British Columbia'
22+
}
23+
}
24+
},
25+
{
26+
...
27+
}
28+
]
2829

29-
const province = into(animals)
30-
.into('pets')
31-
.into('1')
30+
const ownerProvince = into('pets', 0)
3231
.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'
32+
.into('address')
33+
.get('province') //the get method return an array, function, object, string, number... or undefined. Never a error.
34+
console.log(ownerProvince) //log: 'British Columbia'
3635

36+
//with Vanilla JS
3737
const errorPetName = animals.notExist.name //errror: Cannot read property 'name' of undefined
38+
//with getinto package
3839
const petName = into(animals)
3940
.into('notExist')
4041
.get('name')
41-
console.log(petName) //log: undefined
42+
console.log(petName) //log: undefined. Never a error.
4243
```
4344

44-
### Installation
45+
## Installation
4546
```sh
4647
$ npm install getinto
4748
or
4849
$ yarn add getinto
4950
```
5051

51-
### Usage
52-
I suggest the following way to import
52+
## Usage
53+
I suggest the following import:
5354
```js
5455
const into = require('getinto')
5556
```
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+
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).
58+
The nexts intos come from previous into. The latter statement must be 'get'.
59+
THe get method always return an array, function, string, object... or undefined. Never a error (if you use the correct syntax).
5760
```js
58-
const into = require('getinto')
5961
const value = into(object)
60-
.into('objectPropertie')
61-
.into('nextObjectPropertie')
62+
.into('objectProperty')
63+
.into('nextObjectProperty')
6264
...
6365
...
64-
.into('otherPropertie')
65-
.get('returnedPropertie')
66-
console.log(value) //log: returnedPropertie
66+
.into('aIndex')
67+
.get('returnedProperty')
68+
console.log(value) //log: valueOfreturnedProperty
69+
```
70+
## Typescript Usage
71+
I suggest the following import:
72+
```ts
73+
import into from 'getinto'
74+
```
75+
getinto has the same usage for Typescript but, you can to specify the return type on get method.
76+
```ts
77+
into(someObject)
78+
.get('someProperty')<T>
6779
```
68-
### Typescript Usage
69-
Writing
70-
71-
### API
72-
writing the compleate API documentation
7380

74-
### More Examples
81+
## More Examples
7582
```js
7683
const into = require('getinto')
7784
const business = {
@@ -89,61 +96,103 @@ const business = {
8996
}
9097
]
9198
}
92-
93-
business[0].stories.notExist.techs[0] //error: Cannot read property 'techs' of undefined
99+
100+
business.notExist.techs[0] //error: Cannot read property 'techs' of undefined
94101
into(business)
95-
.into('0')
96102
.into('notExist')
97-
.into('techs')
98-
.get('0') //undefined
103+
.get('techs', 0) //undefined
99104

100105
into(business)
101-
.into('stories')
102-
.into('0')
106+
.into('stories', 0)
103107
.get('companyName') // 'megaPet, INC' - if something don't exist return undefined
104108

105109
into(business)
106-
.into('stories')
107-
.into('0')
110+
.into('stories', 0)
108111
.get('products') // ['dogFood', 'dogBed'] - if something don't exist return undefined
109112

110113
into(business)
111-
.into('stories')
112-
.into('0')
113-
.into('stack')
114-
.get('1') // 'node.js' - if something don't exist return undefined
114+
.into('stories', 0)
115+
.get('stack', 1) // 'node.js' - if something don't exist return undefined
115116

116117
const functionExample = into(business)
117-
.into('stories')
118-
.into('0')
118+
.into('stories', 0)
119119
.get('doSomething')
120120
functionExample('Jonathan') //log: 'Jonathan says Hello World' - if something don't exist return undefined
121121

122122
into(business)
123-
.into('stories')
124-
.into('1')
123+
.into('stories', 1)
125124
.get('companyName', 'superMarket') // 'superMarket, INC' - executed like companyName('superMarket')
126125

127126
const productA = 'fishmonger'
128127
into(business)
129-
.into('stories')
130-
.into('1')
131-
.get('products', [productA, 'bakery']) // ['fishmongerProducts', 'bakeryProducts'] - executed like products(productA, 'bakery')
128+
.into('stories', 1)
129+
.get('products', [productA, 'bakery']) // ['fishmongerProduct', 'bakeryProduct'] - executed like products(productA, 'bakery')
132130

133131
into(business)
134-
.into('stories')
135-
.into('1')
132+
.into('stories', 1)
136133
.get('doSomething', []) //log: 'Hello World - executed like doSomething()
137134
}
138135
```
139136

137+
## API
138+
#### • First into
139+
#
140+
```ts
141+
into(entry: Function | Object | Array<any>, params?: any | any[], thisArg?: object): GetintoObject
142+
```
143+
- if entry is an object or any dictionary, you can select a property to continue the chain passing its name as string in params;
144+
- if entry is an array, you can select an item to continue the chain passsaing its position as number or string in params;
145+
- if entry is a function, you can select the function return to continue the chain passaing one param or many params in an array for the function be execute. Use [] (empty array) to execute with no one param.
146+
#### • Nexts into
147+
#
148+
```ts
149+
into(key: string | number, params?: any | any[]): GetintoObject
150+
```
151+
- key can be a property name as string or can be a index as string or number;
152+
- if the key return an object or any dictionary, you can select a property to continue the chain passing its name as string in params;
153+
- if the key return an array, you can select an item to continue the chain passsaing its position as number or string in params;
154+
- if the key return a function, you can select the function return to continue the chain passaing param or array of params for the function be execute. Use [] (empty array) to execute with no one param.
155+
#### • get
156+
#
157+
```ts
158+
get<T>(key: string | number, params?: any | any[], callback?: (gotten: T) => any): T
159+
```
160+
- key can be a property name as string or can be a index as string or number
161+
- if the key return an object or any dictionary, you can get a property passing its name as string in params;
162+
- if the key return an array, you can get an item passsaing its position as number or string in params.
163+
- if you are using typescript, can to specify the type of get return
164+
- if the key return a function, you can get the function return to continue the chain passaing param or array of params for the function be execute. Use [] (empty array) to execute with no one param.
165+
166+
## Tests
167+
We use Jest to test this package.
168+
- Many mixed tests: ok
169+
- Specific Arrays tests: ok
170+
- Specific Objects tests: doing
171+
- Specific funcctions tests: doing
172+
173+
## Releases
174+
**v0.1.0**
175+
Index as number
176+
Improved array and function support
177+
Added tests
178+
Other fixes
179+
**v0.0.2**
180+
Import fixs
181+
**v0.0.1**
182+
Inicial idea
183+
184+
## To-do
185+
- Function tests
186+
- Obkject tests
187+
- More examples
188+
- Search for bugs
189+
140190
### array example
141191
Writing
142192
### object example
143193
Writing
144194
### function example
145195
Writing
146196

147-
### Todos
148-
Writing
197+
149198

0 commit comments

Comments
 (0)