-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multiple new methods to
Object.prototype
- Loading branch information
Ardalan
committed
May 20, 2018
1 parent
14c595a
commit c45d446
Showing
15 changed files
with
356 additions
and
164 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as method from "./method"; | ||
import { addPrototype } from "../utils"; | ||
|
||
declare global { | ||
interface Object { | ||
$camelCaseKeys(): object; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new object from the specified object, where all the keys are in camel-case | ||
* @memberof Object.prototype | ||
* @returns {Object} | ||
* @example | ||
* const myObj = { First_name: "Adam", "last-name": "Smith" }; | ||
* const myObjLower = myObj.$camelCaseKeys(); // {firstName: "Adam", lastName: "Smith"} | ||
*/ | ||
function $camelCaseKeys(this: object): object { | ||
return method(this); | ||
} | ||
|
||
addPrototype("$camelCaseKeys", $camelCaseKeys); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as camelCase from "../../string/camelCase/method"; | ||
|
||
const method = (obj: { [key: string]: any }): object => { | ||
return Object.keys(obj).reduce((acc: { [key: string]: any }, key) => { | ||
acc[camelCase(key)] = obj[key]; | ||
|
||
return acc; | ||
}, {}); | ||
}; | ||
|
||
export = method; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import "./index"; | ||
|
||
describe("Object.prototype.$camelCaseKeys", () => { | ||
test( | ||
"{ First_name: \"Adam\", \"last-name\": \"Smith\" }.$camelCaseKeys() returns " + | ||
"{firstName: \"Adam\", lastName: \"Smith\"}", | ||
() => { | ||
expect({ "First_name": "Adam", "last-name": "Smith" }.$camelCaseKeys()) | ||
.toEqual({ firstName: "Adam", lastName: "Smith" }); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
/** @namespace Object */ | ||
|
||
import "./camelCaseKeys"; | ||
import "./invert"; | ||
import "./isInstance"; | ||
import "./kebabCaseKeys"; | ||
import "./lowerCaseKeys"; | ||
import "./map"; | ||
import "./mapKeys"; | ||
import "./merge"; | ||
import "./size"; | ||
import "./snakeCaseKeys"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as method from "./method"; | ||
import { addPrototype } from "../utils"; | ||
|
||
declare global { | ||
interface Object { | ||
$kebabCaseKeys(): object; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new object from the specified object, where all the keys are in kebab-case | ||
* @memberof Object.prototype | ||
* @returns {Object} | ||
* @example | ||
* const myObj = { First_name: "Adam", lastName: "Smith" }; | ||
* const myObjLower = myObj.$kebabCaseKeys(); // {first-name: "Adam", last-name: "Smith"} | ||
*/ | ||
function $kebabCaseKeys(this: object): object { | ||
return method(this); | ||
} | ||
|
||
addPrototype("$kebabCaseKeys", $kebabCaseKeys); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as kebabCase from "../../string/kebabCase/method"; | ||
|
||
const method = (obj: { [key: string]: any }): object => { | ||
return Object.keys(obj).reduce((acc: { [key: string]: any }, key) => { | ||
acc[kebabCase(key)] = obj[key]; | ||
|
||
return acc; | ||
}, {}); | ||
}; | ||
|
||
export = method; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import "./index"; | ||
|
||
describe("Object.prototype.$kebabCaseKeys", () => { | ||
test( | ||
"{ First_name: \"Adam\", lastName: \"Smith\" }.$kebabCaseKeys() returns " + | ||
"{firstName: \"Adam\", lastName: \"Smith\"}", | ||
() => { | ||
expect({ First_name: "Adam", lastName: "Smith" }.$kebabCaseKeys()) | ||
.toEqual({ "first-name": "Adam", "last-name": "Smith" }); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
import * as camelCaseKeys from "./camelCaseKeys/method"; | ||
import * as invert from "./invert/method"; | ||
import * as isInstance from "./isInstance/method"; | ||
import * as kebabCaseKeys from "./kebabCaseKeys/method"; | ||
import * as lowerCaseKeys from "./lowerCaseKeys/method"; | ||
import * as map from "./map/method"; | ||
import * as mapKeys from "./mapKeys/method"; | ||
import * as merge from "./merge/method"; | ||
import * as size from "./size/method"; | ||
import * as snakeCaseKeys from "./snakeCaseKeys/method"; | ||
|
||
export { | ||
camelCaseKeys, | ||
invert, | ||
isInstance, | ||
kebabCaseKeys, | ||
lowerCaseKeys, | ||
map, | ||
mapKeys, | ||
merge, | ||
size, | ||
snakeCaseKeys, | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as method from "./method"; | ||
import { addPrototype } from "../utils"; | ||
|
||
declare global { | ||
interface Object { | ||
$snakeCaseKeys(): object; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new object from the specified object, where all the keys are in snake-case | ||
* @memberof Object.prototype | ||
* @returns {Object} | ||
* @example | ||
* const myObj = { FirstName: "Adam", "last-name": "Smith" }; | ||
* const myObjLower = myObj.$snakeCaseKeys(); // {first_name: "Adam", last_name: "Smith"} | ||
*/ | ||
function $snakeCaseKeys(this: object): object { | ||
return method(this); | ||
} | ||
|
||
addPrototype("$snakeCaseKeys", $snakeCaseKeys); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as snakeCase from "../../string/snakeCase/method"; | ||
|
||
const method = (obj: { [key: string]: any }): object => { | ||
return Object.keys(obj).reduce((acc: { [key: string]: any }, key) => { | ||
acc[snakeCase(key)] = obj[key]; | ||
|
||
return acc; | ||
}, {}); | ||
}; | ||
|
||
export = method; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import "./index"; | ||
|
||
describe("Object.prototype.$snakeCaseKeys", () => { | ||
test( | ||
"{ FirstName: \"Adam\", \"last-name\": \"Smith\" }.$snakeCaseKeys() returns " + | ||
"{first_name: \"Adam\", last_name: \"Smith\"}", | ||
() => { | ||
expect({ "FirstName": "Adam", "last-name": "Smith" }.$snakeCaseKeys()) | ||
.toEqual({ first_name: "Adam", last_name: "Smith" }); | ||
}); | ||
}); |
Oops, something went wrong.