Skip to content

Commit

Permalink
chore: use builtin flatMap and flat over lodash's flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Aug 8, 2024
1 parent 34a9d94 commit 9ffc57a
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 160 deletions.
28 changes: 11 additions & 17 deletions baker/chunk.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { flatten } from "@ourworldindata/utils"
import chunk from "chunk-text"

export const chunkWords = (text: string, maxChunkLength: number): string[] =>
Expand All @@ -11,13 +10,12 @@ export const chunkSentences = (
// See https://stackoverflow.com/a/25736082/1983739
// Not perfect, just works in most cases
const sentenceRegex = /(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|\n)\s/g
const sentences = flatten(
text
.split(sentenceRegex)
.map((s) =>
s.length > maxChunkLength ? chunkWords(s, maxChunkLength) : s
)
)
const sentences = text
.split(sentenceRegex)
.flatMap((s) =>
s.length > maxChunkLength ? chunkWords(s, maxChunkLength) : s
)

.map((s) => s.trim())
.filter((s) => s)
.reverse() as string[]
Expand Down Expand Up @@ -49,15 +47,11 @@ export const chunkParagraphs = (
text: string,
maxChunkLength: number
): string[] => {
const paragraphs = flatten(
text
.split("\n\n")
.map((p) =>
p.length > maxChunkLength
? chunkSentences(p, maxChunkLength)
: p
)
)
const paragraphs = text
.split("\n\n")
.flatMap((p) =>
p.length > maxChunkLength ? chunkSentences(p, maxChunkLength) : p
)
.map((p) => p.trim())
.filter((p) => p)
.reverse() as string[]
Expand Down
13 changes: 3 additions & 10 deletions db/model/Gdoc/htmlToEnriched.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ import {
EnrichedBlockAllCharts,
} from "@ourworldindata/utils"
import { match, P } from "ts-pattern"
import {
compact,
flatten,
get,
isArray,
isPlainObject,
partition,
} from "lodash"
import { compact, get, isArray, isPlainObject, partition } from "lodash"
import cheerio from "cheerio"
import { spansToSimpleString } from "./gdocUtils.js"

Expand Down Expand Up @@ -1811,8 +1804,8 @@ export function withoutEmptyOrWhitespaceOnlyTextBlocks(
export function joinBlockParseResults<T>(
results: BlockParseResult<T>[]
): BlockParseResult<T> {
const errors = flatten(results.map((r) => r.errors))
const content = flatten(results.map((r) => r.content))
const errors = results.flatMap((r) => r.errors)
const content = results.flatMap((r) => r.content)
return { errors, content }
}

Expand Down
7 changes: 3 additions & 4 deletions explorer/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
DimensionProperty,
excludeUndefined,
exposeInstanceOnWindow,
flatten,
identity,
isInIFrame,
keyBy,
Expand Down Expand Up @@ -1131,9 +1130,9 @@ export class Explorer

@computed get entityPickerColumnDefs(): CoreColumnDef[] {
const allColumnDefs = uniqBy(
flatten(
Array.from(this.explorerProgram.columnDefsByTableSlug.values())
),
Array.from(
this.explorerProgram.columnDefsByTableSlug.values()
).flat(),
(def) => def.slug
)

Expand Down
5 changes: 2 additions & 3 deletions packages/@ourworldindata/core-table/src/CoreTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
range,
difference,
intersection,
flatten,
sum,
uniqBy,
intersectionOfSets,
Expand Down Expand Up @@ -341,7 +340,7 @@ export class CoreTable<
}

toOneDimensionalArray(): any {
return flatten(this.toTypedMatrix().slice(1))
return this.toTypedMatrix().slice(1).flat()
}

private setColumn(def: COL_DEF_TYPE): void {
Expand Down Expand Up @@ -1431,7 +1430,7 @@ export class CoreTable<

concat(tables: CoreTable[], message: string = `Combined tables`): this {
const all = [this, ...tables] as CoreTable[]
const defs = flatten(all.map((table) => table.defs)) as COL_DEF_TYPE[]
const defs = all.flatMap((table) => table.defs) as COL_DEF_TYPE[]
const uniqDefs = uniqBy(defs, (def) => def.slug)
return this.transform(
concatColumnStores(
Expand Down
88 changes: 40 additions & 48 deletions packages/@ourworldindata/core-table/src/OwidTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
intersectionOfSets,
findClosestTimeIndex,
sumBy,
flatten,
uniq,
sortNumeric,
last,
Expand Down Expand Up @@ -155,11 +154,9 @@ export class OwidTable extends CoreTable<OwidRow, OwidColumnDef> {
// todo: should be easy to speed up if necessary.
return sortNumeric(
uniq(
flatten(
this.getColumns(columnSlugs)
.filter((col) => col)
.map((col) => col.uniqTimesAsc)
)
this.getColumns(columnSlugs)
.filter((col) => col)
.flatMap((col) => col.uniqTimesAsc)
)
)
}
Expand Down Expand Up @@ -441,50 +438,45 @@ export class OwidTable extends CoreTable<OwidRow, OwidColumnDef> {
)
return def
})
const newRows = flatten(
Object.values(
groupBy(
this.sortedByTime.rows,
(row) => row[this.entityNameSlug]
)
).map((rowsForSingleEntity) => {
columnSlugs.forEach((valueSlug) => {
let comparisonValue: number
rowsForSingleEntity = rowsForSingleEntity.map(
(row: Readonly<OwidRow>) => {
const newRow = {
...row,
}

const value = row[valueSlug]

if (row[timeColumnSlug] < startTimeBound) {
newRow[valueSlug] =
ErrorValueTypes.MissingValuePlaceholder
} else if (!isNumber(value)) {
newRow[valueSlug] =
ErrorValueTypes.NaNButShouldBeNumber
} else if (comparisonValue !== undefined) {
// Note: comparisonValue can be negative!
// +value / -comparisonValue = negative growth, which is incorrect.
newRow[valueSlug] =
(100 * (value - comparisonValue)) /
Math.abs(comparisonValue)
} else if (value === 0) {
newRow[valueSlug] =
ErrorValueTypes.MissingValuePlaceholder
} else {
comparisonValue = value
newRow[valueSlug] = 0
}

return newRow
const newRows = Object.values(
groupBy(this.sortedByTime.rows, (row) => row[this.entityNameSlug])
).flatMap((rowsForSingleEntity) => {
columnSlugs.forEach((valueSlug) => {
let comparisonValue: number
rowsForSingleEntity = rowsForSingleEntity.map(
(row: Readonly<OwidRow>) => {
const newRow = {
...row,
}
)
})
return rowsForSingleEntity

const value = row[valueSlug]

if (row[timeColumnSlug] < startTimeBound) {
newRow[valueSlug] =
ErrorValueTypes.MissingValuePlaceholder
} else if (!isNumber(value)) {
newRow[valueSlug] =
ErrorValueTypes.NaNButShouldBeNumber
} else if (comparisonValue !== undefined) {
// Note: comparisonValue can be negative!
// +value / -comparisonValue = negative growth, which is incorrect.
newRow[valueSlug] =
(100 * (value - comparisonValue)) /
Math.abs(comparisonValue)
} else if (value === 0) {
newRow[valueSlug] =
ErrorValueTypes.MissingValuePlaceholder
} else {
comparisonValue = value
newRow[valueSlug] = 0
}

return newRow
}
)
})
)
return rowsForSingleEntity
})

return this.transform(
newRows,
Expand Down
10 changes: 2 additions & 8 deletions packages/@ourworldindata/core-table/src/Transforms.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
flatten,
ColumnSlug,
zip,
uniq,
cloneDeep,
} from "@ourworldindata/utils"
import { ColumnSlug, zip, uniq, cloneDeep } from "@ourworldindata/utils"
import {
ErrorValue,
CoreColumnDef,
Expand Down Expand Up @@ -209,7 +203,7 @@ const rollingAverage: Transform = {
currentValues.push(value)
currentTimes.push(time)
}
return flatten(groups)
return groups.flat()
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
sortBy,
exposeInstanceOnWindow,
uniq,
flatten,
Bounds,
DEFAULT_BOUNDS,
Time,
Expand Down Expand Up @@ -683,10 +682,8 @@ export class DiscreteBarChart
}

@computed private get columnsAsSeries(): DiscreteBarItem[] {
return flatten(
this.yColumns.map((col) =>
this.constructSeries(col, col.validRowIndices.slice(0, 1))
)
return this.yColumns.flatMap((col) =>
this.constructSeries(col, col.validRowIndices.slice(0, 1))
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import {
diffDateISOStringInDays,
difference,
flatten,
getYearFromISOStringAndDayOffset,
intersection,
isNumber,
Expand All @@ -46,10 +45,8 @@ export const legacyToOwidTableAndDimensions = (
): { dimensions: OwidChartDimensionInterface[]; table: OwidTable } => {
// Entity meta map

const entityMeta = flatten(
[...json.values()].map(
(value) => value.metadata.dimensions.entities.values
)
const entityMeta = [...json.values()].flatMap(
(value) => value.metadata.dimensions.entities.values
)
const entityMetaById: OwidEntityKey = Object.fromEntries(
entityMeta.map((entity) => [entity.id.toString(), entity])
Expand Down
13 changes: 6 additions & 7 deletions packages/@ourworldindata/grapher/src/facetChart/FacetChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Bounds,
DEFAULT_BOUNDS,
excludeUndefined,
flatten,
getIdealGridParams,
max,
maxBy,
Expand Down Expand Up @@ -719,11 +718,11 @@ export class FacetChart

@computed get numericLegendData(): ColorScaleBin[] {
if (!this.isNumericLegend || !this.hideFacetLegends) return []
const allBins: ColorScaleBin[] = flatten(
this.externalLegends.map((legend) => [
const allBins: ColorScaleBin[] = this.externalLegends.flatMap(
(legend) => [
...(legend.numericLegendData ?? []),
...(legend.categoricalLegendData ?? []),
])
]
)
const uniqBins = this.getUniqBins(allBins)
const sortedBins = sortBy(
Expand All @@ -735,12 +734,12 @@ export class FacetChart

@computed get categoricalLegendData(): CategoricalBin[] {
if (this.isNumericLegend || !this.hideFacetLegends) return []
const allBins: CategoricalBin[] = flatten(
this.externalLegends.map((legend) => [
const allBins: CategoricalBin[] = this.externalLegends
.flatMap((legend) => [
...(legend.numericLegendData ?? []),
...(legend.categoricalLegendData ?? []),
])
).filter((bin) => bin instanceof CategoricalBin) as CategoricalBin[]
.filter((bin) => bin instanceof CategoricalBin) as CategoricalBin[]
const uniqBins = this.getUniqBins(allBins)
const newBins = uniqBins.map(
// remap index to ensure it's unique (the above procedure can lead to duplicates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
min,
max,
last,
flatten,
sum,
dyFromAlign,
removeAllWhitespace,
Expand Down Expand Up @@ -781,7 +780,7 @@ export class HorizontalCategoricalColorLegend extends HorizontalColorLegend {
})
})

return flatten(lines.map((l) => l.marks))
return lines.flatMap((l) => l.marks)
}

@computed get height(): number {
Expand Down
15 changes: 6 additions & 9 deletions packages/@ourworldindata/grapher/src/lineCharts/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getRelativeMouse,
pointsToPath,
minBy,
flatten,
last,
exposeInstanceOnWindow,
round,
Expand Down Expand Up @@ -369,7 +368,7 @@ export class LineChart
}

@computed private get allValues(): LinePoint[] {
return flatten(this.placedSeries.map((series) => series.points))
return this.placedSeries.flatMap((series) => series.points)
}

@observable tooltipState = new TooltipState<{
Expand Down Expand Up @@ -1130,19 +1129,17 @@ export class LineChart
}

@computed get series(): readonly LineChartSeries[] {
return flatten(
this.yColumns.map((col) =>
col.uniqEntityNames.map(
(entityName): LineChartSeries =>
this.constructSingleSeries(entityName, col)
)
return this.yColumns.flatMap((col) =>
col.uniqEntityNames.map(
(entityName): LineChartSeries =>
this.constructSingleSeries(entityName, col)
)
)
}

// TODO: remove, seems unused
@computed get allPoints(): LinePoint[] {
return flatten(this.series.map((series) => series.points))
return this.series.flatMap((series) => series.points)
}

@computed get placedSeries(): PlacedLineChartSeries[] {
Expand Down
Loading

0 comments on commit 9ffc57a

Please sign in to comment.