Skip to content

Commit

Permalink
Add polyfill for 'flatMap' (#1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jan 24, 2019
1 parent 27691d8 commit ff041ce
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
34 changes: 34 additions & 0 deletions src/polyfills/flatMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2019-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

declare function flatMap<T, U>(
list: $ReadOnlyArray<T>,
fn: (item: T, index: number) => $ReadOnlyArray<U> | U,
): Array<U>;

/* eslint-disable no-redeclare */
// $FlowFixMe
const flatMap = Array.prototype.flatMap
? function(list, fn) {
// $FlowFixMe
return Array.prototype.flatMap.call(list, fn);
}
: function(list, fn) {
let result = [];
for (let i = 0; i < list.length; i++) {
const value = fn(list[i]);
if (Array.isArray(value)) {
result = result.concat(value);
} else {
result.push(value);
}
}
return result;
};
export default flatMap;
10 changes: 2 additions & 8 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import find from '../polyfills/find';
import flatMap from '../polyfills/flatMap';
import objectValues from '../polyfills/objectValues';
import objectEntries from '../polyfills/objectEntries';
import {
Expand Down Expand Up @@ -548,14 +549,7 @@ function getAllSubNodes<T: ASTNode, K: ASTNode, L: ASTNode>(
object: SDLDefinedObject<T, K>,
getter: (T | K) => ?(L | $ReadOnlyArray<L>),
): $ReadOnlyArray<L> {
let result = [];
for (const astNode of getAllNodes(object)) {
const subNodes = getter(astNode);
if (subNodes) {
result = result.concat(subNodes);
}
}
return result;
return flatMap(getAllNodes(object), item => getter(item) || []);
}

function getImplementsInterfaceNode(
Expand Down
10 changes: 2 additions & 8 deletions src/utilities/concatAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow strict
*/

import flatMap from '../polyfills/flatMap';
import type { DocumentNode } from '../language/ast';

/**
Expand All @@ -15,15 +16,8 @@ import type { DocumentNode } from '../language/ast';
* GraphQL source files which together represent one conceptual application.
*/
export function concatAST(asts: $ReadOnlyArray<DocumentNode>): DocumentNode {
const batchDefinitions = [];
for (let i = 0; i < asts.length; i++) {
const definitions = asts[i].definitions;
for (let j = 0; j < definitions.length; j++) {
batchDefinitions.push(definitions[j]);
}
}
return {
kind: 'Document',
definitions: batchDefinitions,
definitions: flatMap(asts, ast => ast.definitions),
};
}
12 changes: 1 addition & 11 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow strict
*/

import flatMap from '../polyfills/flatMap';
import objectValues from '../polyfills/objectValues';
import invariant from '../jsutils/invariant';
import mapValue from '../jsutils/mapValue';
Expand Down Expand Up @@ -410,14 +411,3 @@ export function extendSchema(
return extendNamedType(typeDef);
}
}

function flatMap<T, U>(
list: $ReadOnlyArray<T>,
mapFn: T => $ReadOnlyArray<U>,
): Array<U> {
let result = [];
for (const item of list) {
result = result.concat(mapFn(item));
}
return result;
}
24 changes: 8 additions & 16 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow strict
*/

import flatMap from '../polyfills/flatMap';
import objectValues from '../polyfills/objectValues';
import isNullish from '../jsutils/isNullish';
import isInvalid from '../jsutils/isInvalid';
Expand Down Expand Up @@ -392,27 +393,18 @@ function printDescriptionWithComments(lines, indentation, firstInBlock) {
}
function descriptionLines(description: string, maxLen: number): Array<string> {
const lines = [];
const rawLines = description.split('\n');
for (let i = 0; i < rawLines.length; i++) {
if (rawLines[i] === '') {
lines.push(rawLines[i]);
} else {
// For > 120 character long lines, cut at space boundaries into sublines
// of ~80 chars.
const sublines = breakLine(rawLines[i], maxLen);
for (let j = 0; j < sublines.length; j++) {
lines.push(sublines[j]);
}
return flatMap(rawLines, line => {
if (line.length < maxLen + 5) {
return line;
}
}
return lines;
// For > 120 character long lines, cut at space boundaries into sublines
// of ~80 chars.
return breakLine(line, maxLen);
});
}
function breakLine(line: string, maxLen: number): Array<string> {
if (line.length < maxLen + 5) {
return [line];
}
const parts = line.split(new RegExp(`((?: |^).{15,${maxLen - 40}}(?= |$))`));
if (parts.length < 4) {
return [line];
Expand Down

0 comments on commit ff041ce

Please sign in to comment.