Skip to content

Commit

Permalink
Don't use Object.fromEntries
Browse files Browse the repository at this point in the history
It's not available in older node versions.
  • Loading branch information
tvrg committed May 5, 2020
1 parent e915149 commit e550de6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/snage/src/note/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import * as TE from 'fp-ts/lib/TaskEither';
import * as T from 'fp-ts/lib/Task';
import * as E from 'fp-ts/lib/Either';
import * as A from 'fp-ts/lib/Array';
import * as R from 'fp-ts/lib/Record';
import {expectNever, requiredFFVersionRegex} from '../util/util';
import semver from 'semver';
import {pipe} from 'fp-ts/lib/pipeable';
import {ArrayFieldValue, Config, Field, FieldValue, PrimitiveFieldValue} from '../config/type';
import {Note} from './note';
import {readdir, readFile, sequenceKeepAllLefts} from '../fp/fp';
import {getFirstSemigroup} from 'fp-ts/lib/Semigroup';

export const parseNotes = (config: Config, folder: string): TE.TaskEither<Array<FileParseError | string>, Note[]> => {
return pipe(
Expand Down Expand Up @@ -78,7 +80,7 @@ export const parseNote = (fields: Field[], rawNote: RawNote): TE.TaskEither<File
),
T.map(sequenceKeepAllLefts),
TE.map((fieldsWithValue) => ({
values: Object.fromEntries(fieldsWithValue.filter(([, value]) => typeof value !== 'undefined')),
values: toRecord(fieldsWithValue),
id: rawNote.file,
file: rawNote.file,
content: rawNote.content,
Expand All @@ -87,6 +89,15 @@ export const parseNote = (fields: Field[], rawNote: RawNote): TE.TaskEither<File
);
};

const toRecord = <V>(values: Array<[string, V | undefined]>): Record<string, V> => {
const First = getFirstSemigroup<V>();
return pipe(
values,
A.filter((pair): pair is [string, V] => typeof pair[1] !== 'undefined'),
R.fromFoldable(First, A.array)
);
};

export type ParseErrorType = 'missingField' | 'wrongType' | 'invalidSemVer' | 'invalidEnum' | 'invalidFFVersion' | 'providerError';

export interface ParseError {
Expand Down

0 comments on commit e550de6

Please sign in to comment.