Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.md
*.test.js
*.test.ts
.all-contributorsrc
.babelrc.js
.coverage
Expand All @@ -9,14 +12,12 @@
.npmignore
.prettierignore
.prettierrc
*.test.js
*.test.ts
addExtensions.js
compile.js
document.js
ignore.js
normalizeLineEndings.js
package-lock.json
readme.js
regenerate.js
rollup.config.js
*.md
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.coverage
index.cjs.js
index.umd.js
.coverage
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3770,6 +3770,34 @@ mapValues(); // ⇒ TODO
TODO: List questions that may this function answers.
TODO-END -->

#### merge

Merges two objects deeply.

##### Type signature

```
(a: object, b: object) => object
```

##### Examples

```javascript
merge({ a: 1, b: 3 }, {}); // ⇒ { a: 1, b: 3 }
```

```javascript
merge({ a: 1, b: 3 }, { b: 7 }); // ⇒ { a: 1, b: 7 }
```

```javascript
merge({ a: 1, b: 3 }, { b: { d: 8 } }); // ⇒ { a: 1, b: { d: 8 } }
```

```javascript
merge({ a: 1, b: { c: 3 } }, { b: { d: 8 } }); // ⇒ { a: 1, b: { c: 3, d: 8 } }
```

#### none

<!-- TODO-START
Expand Down
1 change: 1 addition & 0 deletions ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ignoredFiles = [
"index.umd.js",
"jest.config.js",
"LICENSE",
"normalizeLineEndings.js",
"package-lock.json",
"package.json",
"readme.js",
Expand Down
72 changes: 72 additions & 0 deletions normalizeLineEndings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-env node */
// eslint-disable console
import { promises } from "fs";
import path from "path";
import os from "os";
import pQueue from "p-queue";

const CONCURRENCY = Math.max(1, os.cpus().length - 1);

const { default: PQueue } = pQueue;

import ignored from "./ignore.js";

const [, ignoredDirectories] = ignored;

const {
readFile: readFileAsync,
writeFile: writeFileAsync,
readdir: readDirectoryAsync
} = promises;

const [, , cwd = process.cwd()] = process.argv;
const root = cwd;

const main = async cwd => {
console.log(`Normalizing files in ${cwd}...`);

const entries = await readDirectoryAsync(cwd, { withFileTypes: true });

const files = entries.filter(x => x.isFile()).map(x => x.name);

const directories = entries
.filter(x => x.isDirectory())
.map(x => x.name)
.filter(x => !ignoredDirectories.includes(x));

for (const directory of directories) {
await main(path.join(cwd, directory));
}

const processFile = async file => {
const filePath = path.join(cwd, file);
const relativeFilePath = path.relative(root, filePath);

try {
console.log(`Normalizing ${relativeFilePath}...`);

const contents = await readFileAsync(filePath, "utf-8");

const normalized = contents
.split("\n")
.map(x => x.trimEnd())
.join("\n");

await writeFileAsync(filePath, normalized, "utf-8");
} catch (error) {
console.error(error);

process.exit(1);
}
};

const queue = new PQueue({ concurrency: CONCURRENCY });

for (const file of files) {
queue.add(() => processFile(file));
}

await queue.onIdle();
};

main(cwd);
28 changes: 28 additions & 0 deletions object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,34 @@ mapValues(); // ⇒ TODO
TODO: List questions that may this function answers.
TODO-END -->

# merge

Merges two objects deeply.

## Type signature

```
(a: object, b: object) => object
```

## Examples

```javascript
merge({ a: 1, b: 3 }, {}); // ⇒ { a: 1, b: 3 }
```

```javascript
merge({ a: 1, b: 3 }, { b: 7 }); // ⇒ { a: 1, b: 7 }
```

```javascript
merge({ a: 1, b: 3 }, { b: { d: 8 } }); // ⇒ { a: 1, b: { d: 8 } }
```

```javascript
merge({ a: 1, b: { c: 3 } }, { b: { d: 8 } }); // ⇒ { a: 1, b: { c: 3, d: 8 } }
```

# none

<!-- TODO-START
Expand Down
2 changes: 1 addition & 1 deletion object/apply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import entries from "./entries";
import fromEntries from "./fromEntries";

export default fs => (...xs) =>
export default (fs: { (...xs: any[]): any }[]) => (...xs: any[]) =>
fromEntries(entries(fs).map(([key, value]) => [key, value(...xs)]));
3 changes: 1 addition & 2 deletions object/entries.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export default Object.entries ||
(object => Object.keys(object).map(key => [key, object[key]]));
export default Object.entries;
3 changes: 1 addition & 2 deletions object/entries.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export default Object.entries ||
(object => Object.keys(object).map(key => [key, object[key]]));
export default Object.entries;
10 changes: 4 additions & 6 deletions object/fromEntries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default Object.fromEntries ||
(keyValuePairs =>
keyValuePairs.reduce(
(acc, [key, value]) => ({ ...acc, [key]: value }),
{}
));
const fromEntries = keyValuePairs =>
keyValuePairs.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});

export default Object.fromEntries || fromEntries;
10 changes: 4 additions & 6 deletions object/fromEntries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default Object.fromEntries ||
(keyValuePairs =>
keyValuePairs.reduce(
(acc, [key, value]) => ({ ...acc, [key]: value }),
{}
));
const fromEntries = (keyValuePairs: [string, any][]): object =>
keyValuePairs.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});

export default Object.fromEntries || fromEntries;
3 changes: 3 additions & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import map from "./map.js";
import mapEntries from "./mapEntries.js";
import mapKeys from "./mapKeys.js";
import mapValues from "./mapValues.js";
import merge from "./merge.js";
import none from "./none.js";
import sort from "./sort.js";

Expand All @@ -44,6 +45,7 @@ export {
mapEntries,
mapKeys,
mapValues,
merge,
none,
sort
};
Expand All @@ -70,6 +72,7 @@ export default {
mapEntries,
mapKeys,
mapValues,
merge,
none,
sort
};
3 changes: 3 additions & 0 deletions object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import map from "./map";
import mapEntries from "./mapEntries";
import mapKeys from "./mapKeys";
import mapValues from "./mapValues";
import merge from "./merge";
import none from "./none";
import sort from "./sort";

Expand All @@ -44,6 +45,7 @@ export {
mapEntries,
mapKeys,
mapValues,
merge,
none,
sort
};
Expand All @@ -70,6 +72,7 @@ export default {
mapEntries,
mapKeys,
mapValues,
merge,
none,
sort
};
4 changes: 3 additions & 1 deletion object/map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fromEntries from "./fromEntries";
import mapEntries from "./mapEntries";

export default f => xs => fromEntries(mapEntries(f)(xs));
export default (f: (value: any, key: string, context: object) => any) => (
xs: object
) => fromEntries(mapEntries(f)(xs));
4 changes: 3 additions & 1 deletion object/mapEntries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import entries from "./entries";

export default f => xs =>
export default (f: (value: any, key: string, context: object) => any) => (
xs: object
): [string, any][] =>
entries(xs).map(([key, value]) => [key, f(value, key, xs)]);
15 changes: 15 additions & 0 deletions object/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import isObject from "../is/object.js";
import map from "./map.js";

const isNonNullishObject = x => x !== undefined && x !== null && isObject(x);

const merge = (a, b) => ({
...a,
...map((value, key) =>
isNonNullishObject(value) && isNonNullishObject(a[key])
? merge(a[key], value)
: value
)(b)
});

export default merge;
27 changes: 27 additions & 0 deletions object/merge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# merge

Merges two objects deeply.

## Type signature

```
(a: object, b: object) => object
```

## Examples

```javascript
merge({ a: 1, b: 3 }, {}); // ⇒ { a: 1, b: 3 }
```

```javascript
merge({ a: 1, b: 3 }, { b: 7 }); // ⇒ { a: 1, b: 7 }
```

```javascript
merge({ a: 1, b: 3 }, { b: { d: 8 } }); // ⇒ { a: 1, b: { d: 8 } }
```

```javascript
merge({ a: 1, b: { c: 3 } }, { b: { d: 8 } }); // ⇒ { a: 1, b: { c: 3, d: 8 } }
```
Loading