Skip to content

Commit

Permalink
fix: simplify function call
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc MacLeod committed Nov 14, 2018
1 parent e02e554 commit 8c0f504
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
22 changes: 14 additions & 8 deletions src/__tests__/parseWithPointers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lineForPosition, yamlParser } from '../parseWithPointers';
import { lineForPosition, parseWithPointers } from '../parseWithPointers';
import * as HugeJSON from './fixtures/huge-json.json';
import { HugeYAML } from './fixtures/huge-yaml';

Expand Down Expand Up @@ -94,42 +94,48 @@ describe('yaml parser', () => {

test('parse simple', () => {
expect(
yamlParser()(`hello: world
parseWithPointers(`hello: world
address:
street: 123`)
).toMatchSnapshot();
});

test('parse diverse', () => {
const result = yamlParser()(diverse);
const result = parseWithPointers(diverse);
delete result.validations;
expect(result).toMatchSnapshot();
});

test('parse huge', () => {
const result = yamlParser()(HugeYAML);
const result = parseWithPointers(HugeYAML);
expect(result.data).toEqual(HugeJSON);
});

test('max depth option', () => {
const result = yamlParser({ maxPointerDepth: 3 })(`prop1: true
const result = parseWithPointers(
`prop1: true
prop2: true
prop3:
prop3-1:
prop3-1-1:
prop3-1-1-1: true
prop3-1-2:
- one
- two`);
- two`,
{ maxPointerDepth: 3 }
);

expect(result.pointers).toMatchSnapshot();
});

test('report errors', () => {
const result = yamlParser({ maxPointerDepth: 3 })(`prop1: true
const result = parseWithPointers(
`prop1: true
prop2: true
inner 1
val: 2`);
val: 2`,
{ maxPointerDepth: 3 }
);

expect(result.validations).toEqual([
{
Expand Down
10 changes: 5 additions & 5 deletions src/parseWithPointers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IParserResult, IParserResultPointers, SourceMapParser } from '@stoplight/types';
import { IParserResult, IParserResultPointers } from '@stoplight/types';
import { load as loadAST, YAMLException } from 'yaml-ast-parser';

// import { IParser, IParserResult, IParserResultPointers, MessageSeverity } from '../../types';
Expand All @@ -10,19 +10,19 @@ export interface IYamlParserOpts {
maxPointerDepth?: number;
}

export const yamlParser = (opts: IYamlParserOpts = {}): SourceMapParser => (input: string) => {
export const parseWithPointers = <T>(value: string, opts: IYamlParserOpts = {}): IParserResult<T> => {
const parsed: IParserResult = {
data: {},
pointers: {},
validations: [],
};

if (!input || !input.trim().length) return parsed;
if (!value || !value.trim().length) return parsed;

const ast = loadAST(input);
const ast = loadAST(value);
if (!ast) return parsed;

const lineMap = computeLineMap(input);
const lineMap = computeLineMap(value);

parsed.pointers = {
'': getLoc(lineMap, {
Expand Down

0 comments on commit 8c0f504

Please sign in to comment.