This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func ConnectToDB() result.Result[sql.DB] { | |
| psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) | |
| return result.NewResult(sql.Open("postgres", psqlInfo)).Try(func(db *sql.DB) error { | |
| return db.Ping() | |
| }).IfOk(func() { | |
| fmt.Println("Successfully connected to database!") | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| obj?.arrayProp?[0]?.someProp?.someFunc?.(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function optional(obj, evalFunc, def) { | |
| const handler = { | |
| get: function(target, prop, receiver) { | |
| const res = Reflect.get(...arguments); | |
| return typeof res === "object" ? proxify(res) : res != null ? res : def; | |
| } | |
| }; | |
| const proxify = target => { | |
| return new Proxy(target, handler); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var object = { 'a': [{ 'b': { 'c': 3 } }] }; | |
| _.get(object, 'a[0].b.c'); // => 3 | |
| _.get(object, ['a', '0', 'b', 'c']); // => 3 | |
| _.get(object, 'a.b.c', 'default'); // => 'default' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function optionalAccess(obj, path, def) { | |
| const propNames = path.replace(/\]|\)/, "").split(/\.|\[|\(/); | |
| return propNames.reduce((acc, prop) => acc[prop] || def, obj); | |
| } | |
| const obj = { | |
| items: [ | |
| { hello: "Hello" } | |
| ] | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function optionalAccess(obj, path, def) { | |
| const propNames = path.replace(/\]|\)/, "").split(/\.|\[|\(/); | |
| return propNames.reduce((acc, prop) => acc[prop] || def, obj); | |
| } | |
| const obj = { | |
| items: [ | |
| { hello: "Hello" } | |
| ] | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log( obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3 ); // => undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let obj; | |
| console.log(obj && obj.someProp); // Prints undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let obj; | |
| console.log(obj.someProp); // => TypeError: Cannot read property 'someProp' of undefined | |
| obj = null; | |
| console.log(obj.someProp); // => TypeError: Cannot read property 'someProp' of null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SomeClass obj; | |
| obj?.prop1?.prop2?.prop3 ?? "SomeDefaultValue"; |
NewerOlder