@tipe/transformer
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,11 @@ | ||
## [1.1.1](https://github.com/tipeio/tipe-transformer/compare/v1.1.0...v1.1.1) (2019-06-28) | ||
### Bug Fixes | ||
* **prettier:** fix lint ([153e0d2](https://github.com/tipeio/tipe-transformer/commit/153e0d2)) | ||
* **test:** fix tests ([79b8d58](https://github.com/tipeio/tipe-transformer/commit/79b8d58)) | ||
* **transformer:** Remove unneccesary validations ([45fa650](https://github.com/tipeio/tipe-transformer/commit/45fa650)) | ||
* **types:** fix types ([4dae6c0](https://github.com/tipeio/tipe-transformer/commit/4dae6c0)) | ||
# [1.1.0](https://github.com/tipeio/tipe-transformer/compare/v1.0.1...v1.1.0) (2019-06-19) | ||
@@ -2,0 +12,0 @@ |
@@ -5,144 +5,30 @@ 'use strict'; | ||
var isString = _interopDefault(require('lodash.isstring')); | ||
var isObject = _interopDefault(require('lodash.isobject')); | ||
var isFunction = _interopDefault(require('lodash.isfunction')); | ||
var isArray = _interopDefault(require('lodash.isarray')); | ||
var reduce = _interopDefault(require('lodash.reduce')); | ||
var keyBy = _interopDefault(require('lodash.keyby')); | ||
var showdown = _interopDefault(require('showdown')); | ||
const TransformerConstants = { | ||
blockDataMissing: 'blocks missing', | ||
missingArguments: 'Transformer needs contain data and a parser', | ||
somethingWentWrong: 'Something went wrong', | ||
invalidParser: 'invalid parser must be a function or Tipe supported parser (currently: html, markdown)' | ||
}; | ||
const transformHTML = (block) => { | ||
const result = reduce(block, (parsedBlock, blockType) => { | ||
switch (blockType) { | ||
case 'text': | ||
parsedBlock = block.content; | ||
break; | ||
case 'button': | ||
parsedBlock = `<button>${block.content}</button>`; | ||
break; | ||
case 'image': | ||
parsedBlock = `<img src="${block.content}" />`.replace(/\\"/g, '"'); | ||
break; | ||
case 'code': | ||
if (block.data) { | ||
parsedBlock = `<pre><code class="${block.data.lang} ${block.data.lang}-css">${block.content}</code></pre>`; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return parsedBlock; | ||
}, ''); | ||
return result; | ||
}; | ||
const markdownConverter = new showdown.Converter(); | ||
const transformMarkdown = (block) => { | ||
let parsedBlock = reduce(block, (parsedBlock, blockType) => { | ||
if (blockType === 'markdown') { | ||
parsedBlock = markdownConverter.makeHtml(block.content); | ||
} | ||
return parsedBlock; | ||
}, ''); | ||
return parsedBlock; | ||
}; | ||
const tipeParsers = { | ||
html: transformHTML, | ||
markdown: transformMarkdown | ||
}; | ||
const validateBlockData = (data) => { | ||
let blockData; | ||
if (data && isString(data)) { | ||
try { | ||
blockData = JSON.parse(data); | ||
} | ||
catch (error) { | ||
throw new Error(error); | ||
} | ||
const transformer = (sections, plugin) => { | ||
let plugins = plugin; | ||
if (!Array.isArray(plugins)) { | ||
// normalize to array | ||
plugins = [plugin]; | ||
} | ||
if (blockData && !blockData.hasOwnProperty('blocks')) | ||
throw new Error(TransformerConstants.blockDataMissing); | ||
if (data && isObject(data)) | ||
return data; | ||
return blockData; | ||
return reduce(sections, (transformedSections, section) => { | ||
let tempSection = { | ||
apiId: section.apiId, | ||
blocks: section.blocks, | ||
results: section.blocks.map((block) => reduce(plugins, (blockResult, currentplugin) => { | ||
let parsedResult = currentplugin(block); | ||
if (parsedResult) { | ||
blockResult = parsedResult; | ||
} | ||
return blockResult; | ||
}, null)) | ||
}; | ||
transformedSections[section.apiId] = tempSection; | ||
return transformedSections; | ||
}, {}); | ||
}; | ||
const validParser = (parser) => { | ||
if (isString(parser) && tipeParsers.hasOwnProperty(parser)) { | ||
return tipeParsers[parser]; | ||
} | ||
if (isFunction(parser)) { | ||
return parser; | ||
} | ||
throw new Error(TransformerConstants.invalidParser); | ||
}; | ||
const handleParserArray = (parserArr, blocks) => { | ||
const result = reduce(blocks, (parsedResult, block) => { | ||
parserArr.forEach((parser) => { | ||
const validParseMethod = validParser(parser); | ||
parsedResult.push(validParseMethod(block)); | ||
}); | ||
return parsedResult.filter(Boolean); | ||
}, []); | ||
return result; | ||
}; | ||
const handleSingleParser = (parser, blocks) => { | ||
const validParseMethod = validParser(parser); | ||
const result = reduce(blocks, (parsedResult, block) => { | ||
parsedResult.push(validParseMethod(block)); | ||
return parsedResult.filter(Boolean); | ||
}, []); | ||
return result; | ||
}; | ||
const transformer = (data, parser) => { | ||
return new Promise((resolve, reject) => { | ||
if (!data || !parser) { | ||
reject(new Error(TransformerConstants.missingArguments)); | ||
} | ||
// handle arrays | ||
if (isArray(parser)) { | ||
const result = Object.keys(data).map((apiId) => { | ||
const parsedSection = { | ||
apiId, | ||
blocks: reduce(validateBlockData(data[apiId]), (parsedBlock, val) => { | ||
if (isArray(val)) { | ||
return handleParserArray(parser, val); | ||
} | ||
return parsedBlock; | ||
}, []) | ||
}; | ||
return parsedSection; | ||
}); | ||
resolve({ | ||
result: { sections: keyBy(result, 'apiId') }, | ||
data: { sections: data } | ||
}); | ||
} | ||
// single parser passed in | ||
const result = Object.keys(data).map((apiId) => { | ||
const parsedSection = { | ||
apiId, | ||
blocks: reduce(validateBlockData(data[apiId]), (parsedBlock, val) => { | ||
if (isArray(val)) { | ||
return handleSingleParser(parser, val); | ||
} | ||
return parsedBlock; | ||
}, []) | ||
}; | ||
return parsedSection; | ||
}); | ||
resolve({ | ||
result: { sections: keyBy(result, 'apiId') }, | ||
data: { sections: data } | ||
}); | ||
}); | ||
}; | ||
module.exports = transformer; |
@@ -1,19 +0,4 @@ | ||
import { ISectionData, ITipeTransformers, IBlock } from './types'; | ||
export declare const tipeParsers: ITipeTransformers; | ||
export declare const validateBlockData: (data: ISectionData) => ISectionData; | ||
export declare const validParser: (parser: string | ((block: IBlock) => string)) => (block: IBlock) => string; | ||
export declare const handleParserArray: (parserArr: string[] | (string | ((block: IBlock) => string))[], blocks: IBlock[]) => string[]; | ||
export declare const handleSingleParser: (parser: string | (() => string), blocks: IBlock[]) => string[]; | ||
export declare const transformer: (data: any, parser: any) => Promise<{ | ||
result: { | ||
sections: { | ||
[type: string]: { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
}; | ||
}; | ||
}; | ||
data: { | ||
sections: ISectionData[]; | ||
}; | ||
}>; | ||
import { ISections, ITransformedSections, TransformerPlugin } from './types'; | ||
import { transformHTML, transformMarkdown } from './transformers'; | ||
export { transformHTML, transformMarkdown }; | ||
export declare const transformer: (sections: ISections, plugin: TransformerPlugin | TransformerPlugin[]) => ITransformedSections; |
@@ -1,13 +0,20 @@ | ||
export interface ISectionData { | ||
[type: string]: { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
}; | ||
export interface ISection { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
} | ||
export interface ITransformedSection extends ISection { | ||
results: any[]; | ||
} | ||
export interface ISections { | ||
[type: string]: ISection; | ||
} | ||
export interface ITransformedSections { | ||
[type: string]: ITransformedSection; | ||
} | ||
export interface IBlockData { | ||
lang?: string; | ||
[field: string]: any; | ||
} | ||
export interface IBlock { | ||
[type: string]: string | any; | ||
apiId: string; | ||
id: string; | ||
type: string; | ||
content: string; | ||
@@ -21,1 +28,2 @@ data?: IBlockData; | ||
} | ||
export declare type TransformerPlugin = (block: IBlock) => any; |
@@ -1,143 +0,29 @@ | ||
import isString from 'lodash.isstring'; | ||
import isObject from 'lodash.isobject'; | ||
import isFunction from 'lodash.isfunction'; | ||
import isArray from 'lodash.isarray'; | ||
import reduce from 'lodash.reduce'; | ||
import keyBy from 'lodash.keyby'; | ||
import showdown from 'showdown'; | ||
const TransformerConstants = { | ||
blockDataMissing: 'blocks missing', | ||
missingArguments: 'Transformer needs contain data and a parser', | ||
somethingWentWrong: 'Something went wrong', | ||
invalidParser: 'invalid parser must be a function or Tipe supported parser (currently: html, markdown)' | ||
}; | ||
const transformHTML = (block) => { | ||
const result = reduce(block, (parsedBlock, blockType) => { | ||
switch (blockType) { | ||
case 'text': | ||
parsedBlock = block.content; | ||
break; | ||
case 'button': | ||
parsedBlock = `<button>${block.content}</button>`; | ||
break; | ||
case 'image': | ||
parsedBlock = `<img src="${block.content}" />`.replace(/\\"/g, '"'); | ||
break; | ||
case 'code': | ||
if (block.data) { | ||
parsedBlock = `<pre><code class="${block.data.lang} ${block.data.lang}-css">${block.content}</code></pre>`; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return parsedBlock; | ||
}, ''); | ||
return result; | ||
}; | ||
const markdownConverter = new showdown.Converter(); | ||
const transformMarkdown = (block) => { | ||
let parsedBlock = reduce(block, (parsedBlock, blockType) => { | ||
if (blockType === 'markdown') { | ||
parsedBlock = markdownConverter.makeHtml(block.content); | ||
} | ||
return parsedBlock; | ||
}, ''); | ||
return parsedBlock; | ||
}; | ||
const tipeParsers = { | ||
html: transformHTML, | ||
markdown: transformMarkdown | ||
}; | ||
const validateBlockData = (data) => { | ||
let blockData; | ||
if (data && isString(data)) { | ||
try { | ||
blockData = JSON.parse(data); | ||
} | ||
catch (error) { | ||
throw new Error(error); | ||
} | ||
const transformer = (sections, plugin) => { | ||
let plugins = plugin; | ||
if (!Array.isArray(plugins)) { | ||
// normalize to array | ||
plugins = [plugin]; | ||
} | ||
if (blockData && !blockData.hasOwnProperty('blocks')) | ||
throw new Error(TransformerConstants.blockDataMissing); | ||
if (data && isObject(data)) | ||
return data; | ||
return blockData; | ||
return reduce(sections, (transformedSections, section) => { | ||
let tempSection = { | ||
apiId: section.apiId, | ||
blocks: section.blocks, | ||
results: section.blocks.map((block) => reduce(plugins, (blockResult, currentplugin) => { | ||
let parsedResult = currentplugin(block); | ||
if (parsedResult) { | ||
blockResult = parsedResult; | ||
} | ||
return blockResult; | ||
}, null)) | ||
}; | ||
transformedSections[section.apiId] = tempSection; | ||
return transformedSections; | ||
}, {}); | ||
}; | ||
const validParser = (parser) => { | ||
if (isString(parser) && tipeParsers.hasOwnProperty(parser)) { | ||
return tipeParsers[parser]; | ||
} | ||
if (isFunction(parser)) { | ||
return parser; | ||
} | ||
throw new Error(TransformerConstants.invalidParser); | ||
}; | ||
const handleParserArray = (parserArr, blocks) => { | ||
const result = reduce(blocks, (parsedResult, block) => { | ||
parserArr.forEach((parser) => { | ||
const validParseMethod = validParser(parser); | ||
parsedResult.push(validParseMethod(block)); | ||
}); | ||
return parsedResult.filter(Boolean); | ||
}, []); | ||
return result; | ||
}; | ||
const handleSingleParser = (parser, blocks) => { | ||
const validParseMethod = validParser(parser); | ||
const result = reduce(blocks, (parsedResult, block) => { | ||
parsedResult.push(validParseMethod(block)); | ||
return parsedResult.filter(Boolean); | ||
}, []); | ||
return result; | ||
}; | ||
const transformer = (data, parser) => { | ||
return new Promise((resolve, reject) => { | ||
if (!data || !parser) { | ||
reject(new Error(TransformerConstants.missingArguments)); | ||
} | ||
// handle arrays | ||
if (isArray(parser)) { | ||
const result = Object.keys(data).map((apiId) => { | ||
const parsedSection = { | ||
apiId, | ||
blocks: reduce(validateBlockData(data[apiId]), (parsedBlock, val) => { | ||
if (isArray(val)) { | ||
return handleParserArray(parser, val); | ||
} | ||
return parsedBlock; | ||
}, []) | ||
}; | ||
return parsedSection; | ||
}); | ||
resolve({ | ||
result: { sections: keyBy(result, 'apiId') }, | ||
data: { sections: data } | ||
}); | ||
} | ||
// single parser passed in | ||
const result = Object.keys(data).map((apiId) => { | ||
const parsedSection = { | ||
apiId, | ||
blocks: reduce(validateBlockData(data[apiId]), (parsedBlock, val) => { | ||
if (isArray(val)) { | ||
return handleSingleParser(parser, val); | ||
} | ||
return parsedBlock; | ||
}, []) | ||
}; | ||
return parsedSection; | ||
}); | ||
resolve({ | ||
result: { sections: keyBy(result, 'apiId') }, | ||
data: { sections: data } | ||
}); | ||
}); | ||
}; | ||
export default transformer; |
@@ -1,19 +0,4 @@ | ||
import { ISectionData, ITipeTransformers, IBlock } from './types'; | ||
export declare const tipeParsers: ITipeTransformers; | ||
export declare const validateBlockData: (data: ISectionData) => ISectionData; | ||
export declare const validParser: (parser: string | ((block: IBlock) => string)) => (block: IBlock) => string; | ||
export declare const handleParserArray: (parserArr: string[] | (string | ((block: IBlock) => string))[], blocks: IBlock[]) => string[]; | ||
export declare const handleSingleParser: (parser: string | (() => string), blocks: IBlock[]) => string[]; | ||
export declare const transformer: (data: any, parser: any) => Promise<{ | ||
result: { | ||
sections: { | ||
[type: string]: { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
}; | ||
}; | ||
}; | ||
data: { | ||
sections: ISectionData[]; | ||
}; | ||
}>; | ||
import { ISections, ITransformedSections, TransformerPlugin } from './types'; | ||
import { transformHTML, transformMarkdown } from './transformers'; | ||
export { transformHTML, transformMarkdown }; | ||
export declare const transformer: (sections: ISections, plugin: TransformerPlugin | TransformerPlugin[]) => ITransformedSections; |
@@ -1,13 +0,20 @@ | ||
export interface ISectionData { | ||
[type: string]: { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
}; | ||
export interface ISection { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
} | ||
export interface ITransformedSection extends ISection { | ||
results: any[]; | ||
} | ||
export interface ISections { | ||
[type: string]: ISection; | ||
} | ||
export interface ITransformedSections { | ||
[type: string]: ITransformedSection; | ||
} | ||
export interface IBlockData { | ||
lang?: string; | ||
[field: string]: any; | ||
} | ||
export interface IBlock { | ||
[type: string]: string | any; | ||
apiId: string; | ||
id: string; | ||
type: string; | ||
content: string; | ||
@@ -21,1 +28,2 @@ data?: IBlockData; | ||
} | ||
export declare type TransformerPlugin = (block: IBlock) => any; |
@@ -1,19 +0,4 @@ | ||
import { ISectionData, ITipeTransformers, IBlock } from './types'; | ||
export declare const tipeParsers: ITipeTransformers; | ||
export declare const validateBlockData: (data: ISectionData) => ISectionData; | ||
export declare const validParser: (parser: string | ((block: IBlock) => string)) => (block: IBlock) => string; | ||
export declare const handleParserArray: (parserArr: string[] | (string | ((block: IBlock) => string))[], blocks: IBlock[]) => string[]; | ||
export declare const handleSingleParser: (parser: string | (() => string), blocks: IBlock[]) => string[]; | ||
export declare const transformer: (data: any, parser: any) => Promise<{ | ||
result: { | ||
sections: { | ||
[type: string]: { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
}; | ||
}; | ||
}; | ||
data: { | ||
sections: ISectionData[]; | ||
}; | ||
}>; | ||
import { ISections, ITransformedSections, TransformerPlugin } from './types'; | ||
import { transformHTML, transformMarkdown } from './transformers'; | ||
export { transformHTML, transformMarkdown }; | ||
export declare const transformer: (sections: ISections, plugin: TransformerPlugin | TransformerPlugin[]) => ITransformedSections; |
@@ -1,13 +0,20 @@ | ||
export interface ISectionData { | ||
[type: string]: { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
}; | ||
export interface ISection { | ||
apiId: string; | ||
blocks: IBlock[]; | ||
} | ||
export interface ITransformedSection extends ISection { | ||
results: any[]; | ||
} | ||
export interface ISections { | ||
[type: string]: ISection; | ||
} | ||
export interface ITransformedSections { | ||
[type: string]: ITransformedSection; | ||
} | ||
export interface IBlockData { | ||
lang?: string; | ||
[field: string]: any; | ||
} | ||
export interface IBlock { | ||
[type: string]: string | any; | ||
apiId: string; | ||
id: string; | ||
type: string; | ||
content: string; | ||
@@ -21,1 +28,2 @@ data?: IBlockData; | ||
} | ||
export declare type TransformerPlugin = (block: IBlock) => any; |
{ | ||
"name": "@tipe/transformer", | ||
"description": "Tipe transformer helpers for Javascript and Node.js", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"main": "dist/cjs/index.js", | ||
@@ -48,6 +48,2 @@ "module": "dist/esm/index.js", | ||
"@types/lodash.isarray": "^4.0.6", | ||
"@types/lodash.isfunction": "^3.0.6", | ||
"@types/lodash.isobject": "^3.0.6", | ||
"@types/lodash.isstring": "^4.0.6", | ||
"@types/lodash.keyby": "^4.6.6", | ||
"@types/lodash.reduce": "^4.6.6", | ||
@@ -83,6 +79,2 @@ "@typescript-eslint/eslint-plugin": "^1.6.0", | ||
"lodash.isarray": "^4.0.0", | ||
"lodash.isfunction": "^3.0.9", | ||
"lodash.isobject": "^3.0.2", | ||
"lodash.isstring": "^4.0.1", | ||
"lodash.keyby": "^4.6.0", | ||
"lodash.reduce": "^4.6.0", | ||
@@ -89,0 +81,0 @@ "showdown": "^1.9.0" |
@@ -8,7 +8,4 @@ import typescript from 'rollup-plugin-typescript2' | ||
const whiteList = { | ||
'lodash.isstring': true, | ||
'lodash.isfunction': true, | ||
'lodash.isobject': true, | ||
'lodash.isarray': true, | ||
'lodash.keyby': true | ||
'lodash.reduce': true | ||
} | ||
@@ -38,7 +35,4 @@ | ||
globals: { | ||
'lodash.isstring': 'isString', | ||
'lodash.isobject': 'isObject', | ||
'lodash.isfunction': 'isFunction', | ||
'lodash.isarray': 'isArray', | ||
'lodash.keyby': 'keyBy' | ||
'lodash.reduce': 'reduce' | ||
} | ||
@@ -45,0 +39,0 @@ } |
import { transformer } from './transformer' | ||
import { TransformerConstants } from './helpers/constants' | ||
import * as mockBlocks from './helpers/mockBlocks.json' | ||
import mockBlocks from './helpers/mockBlocks' | ||
import { IBlock } from './types' | ||
describe('transformer', () => { | ||
it('should take a tipeParser name as a string and use the corresponding tipeParser', async () => { | ||
const parsedValue = await transformer(mockBlocks, 'html') | ||
const apiId = "hero" | ||
expect(parsedValue.result.sections[apiId]).toBeTruthy() | ||
it('should use a parser function if passed in', () => { | ||
const mockFunction = jest.fn() | ||
transformer(mockBlocks, mockFunction) | ||
expect(mockFunction).toHaveBeenCalled() | ||
}) | ||
it('should throw if the parser argument is a string and does not map to a tipeParser', async () => { | ||
const htmlCaller = async () => { | ||
await transformer(mockBlocks, 'foo') | ||
} | ||
it('should use multiple parser functions if passed in', () => { | ||
const mockFunction1 = jest.fn() | ||
const mockFunction2 = jest.fn() | ||
transformer(mockBlocks, [mockFunction1, mockFunction2]) | ||
expect(mockFunction1).toHaveBeenCalled() | ||
expect(mockFunction2).toHaveBeenCalled() | ||
}) | ||
await expect(htmlCaller()).rejects.toThrow(new Error(TransformerConstants.invalidParser)) | ||
it('should return the last defined value', () => { | ||
const mockFunction1 = jest.fn().mockReturnValue('foo') | ||
const mockFunction2 = jest.fn().mockReturnValue(undefined) | ||
const mockFunction3 = jest.fn().mockReturnValue('bar') | ||
const result = transformer({ | ||
Hero: { | ||
apiId: 'Hero', | ||
blocks: [(1) as unknown as IBlock] | ||
} | ||
}, [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result.Hero.results[0]).toBe('bar') | ||
}) | ||
it('should use a parser function if passed in', () => { | ||
const mockFunction = jest.fn() | ||
transformer(mockBlocks, mockFunction) | ||
expect(mockFunction).toHaveBeenCalled() | ||
it('should return the last defined truthy value', () => { | ||
const mockFunction1 = jest.fn().mockReturnValue('foo') | ||
const mockFunction2 = jest.fn().mockReturnValue(undefined) | ||
const mockFunction3 = jest.fn().mockReturnValue(undefined) | ||
const result = transformer({ | ||
Hero: { | ||
apiId: 'Hero', | ||
blocks: [(1) as unknown as IBlock] | ||
} | ||
}, [mockFunction1, mockFunction2, mockFunction3]) | ||
expect(result.Hero.results[0]).toBe('foo') | ||
}) | ||
}) |
@@ -1,153 +0,54 @@ | ||
import isString from 'lodash.isstring' | ||
import isObject from 'lodash.isobject' | ||
import isFunction from 'lodash.isfunction' | ||
import isArray from 'lodash.isarray' | ||
import reduce from 'lodash.reduce' | ||
import keyBy from 'lodash.keyby' | ||
import { ISectionData, ITipeTransformers, IBlock } from './types' | ||
import { TransformerConstants } from './helpers/constants' | ||
import { | ||
ISections, | ||
ITransformedSections, | ||
IBlock, | ||
TransformerPlugin, | ||
ISection | ||
} from './types' | ||
import { transformHTML, transformMarkdown } from './transformers' | ||
export { transformHTML, transformMarkdown } | ||
export const tipeParsers: ITipeTransformers = { | ||
html: transformHTML, | ||
markdown: transformMarkdown | ||
} | ||
export const transformer = ( | ||
sections: ISections, | ||
plugin: TransformerPlugin | TransformerPlugin[] | ||
): ITransformedSections => { | ||
let plugins: TransformerPlugin[] = plugin as TransformerPlugin[] | ||
export const validateBlockData = (data: ISectionData): ISectionData => { | ||
let blockData | ||
if (data && isString(data)) { | ||
try { | ||
blockData = JSON.parse(data) | ||
} catch (error) { | ||
throw new Error(error) | ||
} | ||
if (!Array.isArray(plugins)) { | ||
// normalize to array | ||
plugins = [plugin as TransformerPlugin] | ||
} | ||
if (blockData && !blockData.hasOwnProperty('blocks')) | ||
throw new Error(TransformerConstants.blockDataMissing) | ||
return reduce( | ||
sections, | ||
( | ||
transformedSections: ITransformedSections, | ||
section: ISection | ||
): ITransformedSections => { | ||
let tempSection = { | ||
apiId: section.apiId, | ||
blocks: section.blocks, | ||
results: section.blocks.map((block: IBlock) => | ||
reduce( | ||
plugins, | ||
(blockResult: any, currentplugin: TransformerPlugin): IBlock => { | ||
let parsedResult = currentplugin(block) | ||
if (data && isObject(data)) return data | ||
if (parsedResult) { | ||
blockResult = parsedResult | ||
} | ||
return blockData | ||
} | ||
export const validParser = ( | ||
parser: string | ((block: IBlock) => string) | ||
): ((block: IBlock) => string) => { | ||
if (isString(parser) && tipeParsers.hasOwnProperty(parser)) { | ||
return tipeParsers[parser] | ||
} | ||
if (isFunction(parser)) { | ||
return parser | ||
} | ||
throw new Error(TransformerConstants.invalidParser) | ||
} | ||
export const handleParserArray = ( | ||
parserArr: string[] | (string | ((block: IBlock) => string))[], | ||
blocks: IBlock[] | ||
): string[] => { | ||
const result = reduce( | ||
blocks, | ||
(parsedResult: string[], block: IBlock): string[] => { | ||
parserArr.forEach( | ||
(parser): void => { | ||
const validParseMethod = validParser(parser) | ||
parsedResult.push(validParseMethod(block)) | ||
} | ||
) | ||
return parsedResult.filter(Boolean) | ||
}, | ||
[] | ||
) | ||
return result | ||
} | ||
export const handleSingleParser = ( | ||
parser: string | (() => string), | ||
blocks: IBlock[] | ||
): string[] => { | ||
const validParseMethod = validParser(parser) | ||
const result = reduce( | ||
blocks, | ||
(parsedResult: string[], block: IBlock): string[] => { | ||
parsedResult.push(validParseMethod(block)) | ||
return parsedResult.filter(Boolean) | ||
}, | ||
[] | ||
) | ||
return result | ||
} | ||
export const transformer = ( | ||
data: any, | ||
parser: string | (() => string) | any | ||
): Promise<{ | ||
result: { sections: { [type: string]: { apiId: string; blocks: IBlock[] } } } | ||
data: { sections: ISectionData[] } | ||
}> => { | ||
return new Promise( | ||
(resolve, reject): any => { | ||
if (!data || !parser) { | ||
reject(new Error(TransformerConstants.missingArguments)) | ||
} | ||
// handle arrays | ||
if (isArray(parser)) { | ||
const result = Object.keys(data).map( | ||
(apiId: string): any => { | ||
const parsedSection = { | ||
apiId, | ||
blocks: reduce( | ||
validateBlockData(data[apiId]), | ||
(parsedBlock: string[], val: any): string[] => { | ||
if (isArray(val)) { | ||
return handleParserArray(parser, val) | ||
} | ||
return parsedBlock | ||
}, | ||
[] | ||
) | ||
} | ||
return parsedSection | ||
} | ||
return blockResult | ||
}, | ||
null | ||
) | ||
) | ||
resolve({ | ||
result: { sections: keyBy(result, 'apiId') }, | ||
data: { sections: data } | ||
}) | ||
} | ||
// single parser passed in | ||
const result = Object.keys(data).map( | ||
(apiId: string): any => { | ||
const parsedSection = { | ||
apiId, | ||
blocks: reduce( | ||
validateBlockData(data[apiId]), | ||
(parsedBlock, val): any => { | ||
if (isArray(val)) { | ||
return handleSingleParser(parser, val) | ||
} | ||
return parsedBlock | ||
}, | ||
[] | ||
) | ||
} | ||
return parsedSection | ||
} | ||
) | ||
resolve({ | ||
result: { sections: keyBy(result, 'apiId') }, | ||
data: { sections: data } | ||
}) | ||
} | ||
transformedSections[section.apiId] = tempSection | ||
return transformedSections | ||
}, | ||
{} | ||
) | ||
} |
@@ -10,3 +10,3 @@ import { transformHTML } from './transformHTML' | ||
blocks: [{ | ||
apiId: 'asdfasdf', | ||
id: 'asdfasdf', | ||
content: `<p>sdfasdfasdf</p>`, | ||
@@ -26,3 +26,3 @@ type: `text` | ||
blocks: [{ | ||
apiId: 'asdfasdf', | ||
id: 'asdfasdf', | ||
content: `button cta`, | ||
@@ -42,3 +42,3 @@ type: `button` | ||
blocks: [{ | ||
apiId: 'asdfasdf', | ||
id: 'asdfasdf', | ||
content: `https://dev.cdn.tipe.io/adfasdfasdf`, | ||
@@ -60,3 +60,3 @@ type: `image` | ||
blocks: [{ | ||
apiId: 'asdfasdf', | ||
id: 'asdfasdf', | ||
content: `var tipe = clean`, | ||
@@ -63,0 +63,0 @@ type: `code`, |
@@ -8,3 +8,3 @@ import { transformMarkdown } from './transformMarkdown' | ||
blocks: [{ | ||
apiId: 'asdfasdf', | ||
id: 'asdfasdf', | ||
content: `### header 3`, | ||
@@ -11,0 +11,0 @@ type: `markdown` |
@@ -1,14 +0,25 @@ | ||
export interface ISectionData { | ||
[type: string]: { | ||
apiId: string | ||
blocks: IBlock[] | ||
} | ||
export interface ISection { | ||
apiId: string | ||
blocks: IBlock[] | ||
} | ||
export interface ITransformedSection extends ISection { | ||
results: any[] | ||
} | ||
export interface ISections { | ||
[type: string]: ISection | ||
} | ||
export interface ITransformedSections { | ||
[type: string]: ITransformedSection | ||
} | ||
export interface IBlockData { | ||
lang?: string | ||
[field: string]: any | ||
} | ||
export interface IBlock { | ||
[type: string]: string | any | ||
apiId: string | ||
id: string | ||
type: string | ||
content: string | ||
@@ -23,1 +34,3 @@ data?: IBlockData | ||
} | ||
export type TransformerPlugin = (block: IBlock) => any |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
4
32
61
0
490134
970
- Removedlodash.isfunction@^3.0.9
- Removedlodash.isobject@^3.0.2
- Removedlodash.isstring@^4.0.1
- Removedlodash.keyby@^4.6.0
- Removedlodash.isfunction@3.0.9(transitive)
- Removedlodash.isobject@3.0.2(transitive)
- Removedlodash.isstring@4.0.1(transitive)
- Removedlodash.keyby@4.6.0(transitive)