@tipe/transformer
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -0,1 +1,9 @@ | ||
## [1.1.2](https://github.com/tipeio/tipe-transformer/compare/v1.1.1...v1.1.2) (2019-08-07) | ||
### Bug Fixes | ||
* **linting:** Update linting ([c4afc50](https://github.com/tipeio/tipe-transformer/commit/c4afc50)) | ||
* **ouput:** Change transformer exported name ([9271319](https://github.com/tipeio/tipe-transformer/commit/9271319)) | ||
## [1.1.1](https://github.com/tipeio/tipe-transformer/compare/v1.1.0...v1.1.1) (2019-06-28) | ||
@@ -2,0 +10,0 @@ |
{ | ||
"name": "@tipe/transformer", | ||
"description": "Tipe transformer helpers for Javascript and Node.js", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"main": "dist/cjs/index.js", | ||
@@ -9,3 +9,3 @@ "module": "dist/esm/index.js", | ||
"scripts": { | ||
"lint": "eslint 'src/**/*.ts'", | ||
"lint": "eslint --fix 'src/**/*.ts'", | ||
"prebuild": "rimraf dist", | ||
@@ -12,0 +12,0 @@ "build": "rimraf dist && rollup -c", |
@@ -1,5 +0,6 @@ | ||
import { ISections } from '../types' | ||
import { ISection } from '../types' | ||
const sections: ISections = { | ||
hero: { | ||
const sections: ISection[] = [ | ||
{ | ||
name: 'hero', | ||
apiId: 'hero', | ||
@@ -34,3 +35,4 @@ blocks: [ | ||
}, | ||
body: { | ||
{ | ||
name: 'body', | ||
apiId: 'body', | ||
@@ -65,4 +67,4 @@ blocks: [ | ||
} | ||
} | ||
] | ||
export default sections |
@@ -1,10 +0,8 @@ | ||
import { transformer } from './transformer' | ||
import { TransformerConstants } from './helpers/constants' | ||
import { transform } from './transformer' | ||
import mockBlocks from './helpers/mockBlocks' | ||
import { IBlock } from './types' | ||
describe('transformer', () => { | ||
describe('transform', () => { | ||
it('should use a parser function if passed in', () => { | ||
const mockFunction = jest.fn() | ||
transformer(mockBlocks, mockFunction) | ||
transform(mockBlocks, mockFunction) | ||
expect(mockFunction).toHaveBeenCalled() | ||
@@ -16,3 +14,3 @@ }) | ||
const mockFunction2 = jest.fn() | ||
transformer(mockBlocks, [mockFunction1, mockFunction2]) | ||
transform(mockBlocks, [mockFunction1, mockFunction2]) | ||
expect(mockFunction1).toHaveBeenCalled() | ||
@@ -26,9 +24,15 @@ expect(mockFunction2).toHaveBeenCalled() | ||
const mockFunction3 = jest.fn().mockReturnValue('bar') | ||
const result = transformer({ | ||
Hero: { | ||
apiId: 'Hero', | ||
blocks: [(1) as unknown as IBlock] | ||
const apiId = 'Hero' | ||
const result = transform([ | ||
{ | ||
name: 'Hero', | ||
apiId, | ||
blocks: [{ | ||
id: 'title', | ||
content: '<h1>Tipe CMS</h1>', | ||
type: 'text' | ||
}] | ||
} | ||
}, [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result.Hero.results[0]).toBe('bar') | ||
], [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result[apiId].blocks[0].result).toBe('bar') | ||
}) | ||
@@ -40,10 +44,56 @@ | ||
const mockFunction3 = jest.fn().mockReturnValue(undefined) | ||
const result = transformer({ | ||
Hero: { | ||
apiId: 'Hero', | ||
blocks: [(1) as unknown as IBlock] | ||
const apiId = 'Hero' | ||
const result = transform([ | ||
{ | ||
name: 'Hero', | ||
apiId, | ||
blocks: [{ | ||
id: 'title', | ||
content: '<h1>Tipe CMS</h1>', | ||
type: 'text' | ||
}] | ||
} | ||
}, [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result.Hero.results[0]).toBe('foo') | ||
], [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result[apiId].blocks[0].result).toBe('foo') | ||
}) | ||
it('should return the original block and result', () => { | ||
const mockFunction1 = jest.fn().mockReturnValue('foo') | ||
const mockFunction2 = jest.fn().mockReturnValue(undefined) | ||
const mockFunction3 = jest.fn().mockReturnValue(undefined) | ||
const apiId = 'Hero' | ||
const block = { | ||
id: 'title', | ||
content: '<h1>Tipe CMS</h1>', | ||
type: 'text' | ||
} | ||
const result = transform([ | ||
{ | ||
name: 'Hero', | ||
apiId, | ||
blocks: [block] | ||
} | ||
], [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result[apiId].blocks[0].result).toBe('foo') | ||
expect(result[apiId].blocks[0].block).toBe(block) | ||
}) | ||
it('should format the sections to be keyed by apiId', () => { | ||
const mockFunction1 = jest.fn().mockReturnValue('foo') | ||
const mockFunction2 = jest.fn().mockReturnValue(undefined) | ||
const mockFunction3 = jest.fn().mockReturnValue(undefined) | ||
const apiId = 'Hero' | ||
const result = transform([ | ||
{ | ||
name: 'Hero', | ||
apiId, | ||
blocks: [{ | ||
id: 'title', | ||
content: '<h1>Tipe CMS</h1>', | ||
type: 'text' | ||
}] | ||
} | ||
], [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result[apiId]).toBeTruthy() | ||
}) | ||
}) |
import reduce from 'lodash.reduce' | ||
import { | ||
ISections, | ||
ITransformedSections, | ||
IBlock, | ||
TransformerPlugin, | ||
ITransformedSections, | ||
IParsedSection, | ||
IBlockResult, | ||
ISection | ||
@@ -12,4 +13,4 @@ } from './types' | ||
export const transformer = ( | ||
sections: ISections, | ||
export const transform = ( | ||
sections: ISection[], | ||
plugin: TransformerPlugin | TransformerPlugin[] | ||
@@ -27,16 +28,19 @@ ): ITransformedSections => { | ||
( | ||
transformedSections: ITransformedSections, | ||
transformedSections: ITransformedSections[] | any, | ||
section: ISection | ||
): ITransformedSections => { | ||
): IParsedSection[] | ISection[] => { | ||
let tempSection = { | ||
name: section.name, | ||
apiId: section.apiId, | ||
blocks: section.blocks, | ||
results: section.blocks.map((block: IBlock) => | ||
blocks: section.blocks.map((block: IBlock): IBlockResult[] | null => | ||
reduce( | ||
plugins, | ||
(blockResult: any, currentplugin: TransformerPlugin): IBlock => { | ||
( | ||
blockResult: IBlockResult | any, | ||
currentplugin: TransformerPlugin | ||
): IBlockResult[] => { | ||
let parsedResult = currentplugin(block) | ||
if (parsedResult) { | ||
blockResult = parsedResult | ||
blockResult = { block, result: parsedResult } | ||
} | ||
@@ -43,0 +47,0 @@ |
import { transformHTML } from './transformHTML' | ||
import { TransformerConstants } from '../../helpers/constants' | ||
@@ -4,0 +3,0 @@ describe('transformerHTML', () => { |
import reduce from 'lodash.reduce' | ||
import { IBlock } from '../../types' | ||
import { IBlock, IBlockType } from '../../types' | ||
@@ -7,3 +7,3 @@ export const transformHTML = (block: IBlock): string => { | ||
block, | ||
(parsedBlock: string, blockType: any): string => { | ||
(parsedBlock: string, blockType: IBlockType): string => { | ||
switch (blockType) { | ||
@@ -10,0 +10,0 @@ case 'text': |
import reduce from 'lodash.reduce' | ||
import showdown from 'showdown' | ||
import { IBlock } from '../../types' | ||
import { IBlock, IBlockType } from '../../types' | ||
@@ -10,3 +10,3 @@ const markdownConverter = new showdown.Converter() | ||
block, | ||
(parsedBlock: string, blockType: any): string => { | ||
(parsedBlock: string, blockType: IBlockType): string => { | ||
if (blockType === 'markdown') { | ||
@@ -13,0 +13,0 @@ parsedBlock = markdownConverter.makeHtml(block.content) |
export interface ISection { | ||
name: string | ||
apiId: string | ||
@@ -6,16 +7,14 @@ blocks: IBlock[] | ||
export interface ITransformedSection extends ISection { | ||
results: any[] | ||
export interface IParsedSection { | ||
name: string | ||
apiId: string | ||
blocks: IBlockResult[] | ||
} | ||
export interface ISections { | ||
[type: string]: ISection | ||
} | ||
export interface ITransformedSections { | ||
[type: string]: ITransformedSection | ||
[key: string]: IParsedSection | ||
} | ||
export interface IBlockData { | ||
[field: string]: any | ||
export interface ISections { | ||
[type: string]: ISection | ||
} | ||
@@ -27,5 +26,16 @@ | ||
content: string | ||
data?: IBlockData | ||
data?: any | ||
} | ||
export interface IParsedBlock { | ||
[type: string]: any | ||
} | ||
export interface IBlockResult { | ||
block: IBlock | ||
result: IParsedBlock | string | ||
} | ||
export type IBlockType = 'image' | 'text' | 'markdown' | 'button' | 'code' | ||
export interface ITipeTransformers { | ||
@@ -32,0 +42,0 @@ [type: string]: (block: IBlock) => string |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
398127
25
480