Skip to content

Commit 2642943

Browse files
committed
Merge branch 'develop' into main
2 parents 3a354a0 + 599e0ab commit 2642943

12 files changed

Lines changed: 1035 additions & 15 deletions

File tree

.gitignore

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

.npmignore

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

LICENSE.md

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

README.md

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,149 @@
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+

lib/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface getintoObject {
2+
into(key: string, params?: string | string[]): getintoObject;
3+
get<T>(key: string, params?: string | string[], callback?: (gotten: T) => any): T;
4+
}
5+
interface ObjectOrFunction {
6+
[key: string]: Object | Function | Array<any>;
7+
}
8+
declare function intoInitiator(anyType: ObjectOrFunction, params?: string | string[], thisArg?: object): getintoObject;
9+
export default intoInitiator;
10+
//# sourceMappingURL=index.d.ts.map

lib/index.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "getinto",
3+
"version": "1.0.0",
4+
"description": "A simple JS package to access a value in a object",
5+
"main": "./lib/index.js",
6+
"types": "./lib/index.d.ts",
7+
"author": "Jonathan R C <jonarc06@gmail.com>",
8+
"license": "MIT",
9+
"scripts": {
10+
"dev": "ts-node-dev --respawn --transpile-only src/index.ts",
11+
"build": "tsc"
12+
},
13+
"devDependencies": {
14+
"ts-node-dev": "^1.1.1",
15+
"typescript": "^4.1.3"
16+
},
17+
"dependencies": {},
18+
"repository": {
19+
"type": "git",
20+
"url": "https://github.com/JonRC/getinto.git"
21+
},
22+
"keywords": [
23+
"deep",
24+
"get",
25+
"propertie",
26+
"properties",
27+
"function",
28+
"array",
29+
"in",
30+
"into",
31+
"object",
32+
"objects"
33+
],
34+
"bugs": {
35+
"url": "https://github.com/JonRC/getinto/issues"
36+
},
37+
"homepage": "https://github.com/JonRC/getinto"
38+
}

src/index.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
interface getintoObject {
3+
into(key: string, params?: string | string[]): getintoObject
4+
get<T>(key: string, params?: string | string[], callback?: (gotten: T) => any): T
5+
6+
}
7+
8+
interface ObjectOrFunction {
9+
[key: string]: Object | Function | Array<any>;
10+
}
11+
12+
function intoInitiator(anyType: ObjectOrFunction, params?: string | string[], thisArg?: object): getintoObject {
13+
anyType = functionVerifier(anyType, params, thisArg)
14+
return intoContructor(anyType)
15+
}
16+
17+
function intoContructor(anyType: ObjectOrFunction): getintoObject {
18+
function getInto(anyType: ObjectOrFunction, key: string, params?: string | string[]) {
19+
if (anyType) {
20+
if (anyType instanceof Array) {
21+
key = key.replace('[', '').replace(']', '')
22+
const value = anyType[key]
23+
return functionVerifier(value, params, anyType)
24+
}
25+
else if (anyType instanceof Object) {
26+
const value = anyType[key]
27+
return functionVerifier(value, params, anyType)
28+
}
29+
else {
30+
return undefined
31+
}
32+
}
33+
else {
34+
return undefined
35+
}
36+
}
37+
38+
39+
return {
40+
into: (key, params) => {
41+
return intoContructor(getInto(anyType, key, params))
42+
},
43+
44+
get: (key, params, callback?) => {
45+
const value = getInto(anyType, key, params)
46+
if(callback instanceof Function) callback(value)
47+
return value
48+
},
49+
50+
}
51+
}
52+
53+
function functionVerifier(anyValue: Object | Function, params?: string | string[], thisArg?: any) {
54+
if (anyValue instanceof Function && params) {
55+
if (params instanceof Array) {
56+
return anyValue.bind(thisArg)(...params)
57+
}
58+
else {
59+
return anyValue.bind(thisArg)(params)
60+
}
61+
}
62+
63+
else if(anyValue instanceof Function){
64+
return anyValue.bind(thisArg)
65+
}
66+
67+
else {
68+
return anyValue
69+
}
70+
}
71+
72+
export default intoInitiator

0 commit comments

Comments
 (0)