Skip to content

Commit

Permalink
Added multiple new methods to Object.prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan committed May 20, 2018
1 parent 14c595a commit c45d446
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 164 deletions.
36 changes: 36 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1234,15 +1234,29 @@ Number.isInstance(2); // true

* [Object](#Object) : <code>object</code>
* _instance_
* [.$camelCaseKeys()](#Object+$camelCaseKeys)[<code>Object</code>](#Object)
* [.$invert()](#Object+$invert)[<code>Object</code>](#Object)
* [.$kebabCaseKeys()](#Object+$kebabCaseKeys)[<code>Object</code>](#Object)
* [.$lowerCaseKeys()](#Object+$lowerCaseKeys)[<code>Object</code>](#Object)
* [.$map(fn)](#Object+$map)[<code>Object</code>](#Object)
* [.$mapKeys(fn)](#Object+$mapKeys)[<code>Object</code>](#Object)
* [.$merge(...objects)](#Object+$merge)[<code>Object</code>](#Object)
* [.$size()](#Object+$size)[<code>Object</code>](#Object)
* [.$snakeCaseKeys()](#Object+$snakeCaseKeys)[<code>Object</code>](#Object)
* _static_
* [.isInstance](#Object.isInstance) ⇒ <code>boolean</code>

<a name="Object+$camelCaseKeys"></a>

### object.$camelCaseKeys() ⇒ [<code>Object</code>](#Object)
Creates a new object from the specified object, where all the keys are in camel-case

**Kind**: instance method of [<code>Object</code>](#Object)
**Example**
```javascript
const myObj = { First_name: "Adam", "last-name": "Smith" };
const myObjLower = myObj.$camelCaseKeys(); // {firstName: "Adam", lastName: "Smith"}
```
<a name="Object+$invert"></a>

### object.$invert() ⇒ [<code>Object</code>](#Object)
Expand All @@ -1253,6 +1267,17 @@ Inverts the key-value pairs of the object, without mutating it
```javascript
{ name: "John", age: 20 }.$invert(); // { 20: "age", John: "name" }
```
<a name="Object+$kebabCaseKeys"></a>

### object.$kebabCaseKeys() ⇒ [<code>Object</code>](#Object)
Creates a new object from the specified object, where all the keys are in kebab-case

**Kind**: instance method of [<code>Object</code>](#Object)
**Example**
```javascript
const myObj = { First_name: "Adam", lastName: "Smith" };
const myObjLower = myObj.$kebabCaseKeys(); // {first-name: "Adam", last-name: "Smith"}
```
<a name="Object+$lowerCaseKeys"></a>

### object.$lowerCaseKeys() ⇒ [<code>Object</code>](#Object)
Expand Down Expand Up @@ -1334,6 +1359,17 @@ Get size of the object
```javascript
{ one: 1, two: 2, three: 3 }.$size(); // 3
```
<a name="Object+$snakeCaseKeys"></a>

### object.$snakeCaseKeys() ⇒ [<code>Object</code>](#Object)
Creates a new object from the specified object, where all the keys are in snake-case

**Kind**: instance method of [<code>Object</code>](#Object)
**Example**
```javascript
const myObj = { FirstName: "Adam", "last-name": "Smith" };
const myObjLower = myObj.$snakeCaseKeys(); // {first_name: "Adam", last_name: "Smith"}
```
<a name="Object.isInstance"></a>

### Object.isInstance ⇒ <code>boolean</code>
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog


## [v0.7.0](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.7.0) *(2018-05-20)*
**Implemented enhancements:**
- `Object.prototype`
- function `camelCaseKeys` added
- function `kebabCaseKeys` added
- function `snakeCaseKeys` added


## [v0.6.0](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.6.0) *(2018-05-11)*
**Implemented enhancements:**
- `Array.prototype`
Expand Down
22 changes: 22 additions & 0 deletions lib/object/camelCaseKeys/index.ts
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);
11 changes: 11 additions & 0 deletions lib/object/camelCaseKeys/method.ts
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;
11 changes: 11 additions & 0 deletions lib/object/camelCaseKeys/test.ts
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" });
});
});
3 changes: 3 additions & 0 deletions lib/object/index.ts
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";
22 changes: 22 additions & 0 deletions lib/object/kebabCaseKeys/index.ts
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);
11 changes: 11 additions & 0 deletions lib/object/kebabCaseKeys/method.ts
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;
11 changes: 11 additions & 0 deletions lib/object/kebabCaseKeys/test.ts
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" });
});
});
6 changes: 6 additions & 0 deletions lib/object/methods.ts
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,
};
22 changes: 22 additions & 0 deletions lib/object/snakeCaseKeys/index.ts
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);
11 changes: 11 additions & 0 deletions lib/object/snakeCaseKeys/method.ts
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;
11 changes: 11 additions & 0 deletions lib/object/snakeCaseKeys/test.ts
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" });
});
});
Loading

0 comments on commit c45d446

Please sign in to comment.