@resoc/core
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,4 +0,4 @@ | ||
export { DefaultManifestName, renderTemplateToHtml, demoParamValues, paramLabel, validateParamValue, FacebookOpenGraph, TwitterCard } from "./template"; | ||
export { loadRemoteTemplate } from "./remote"; | ||
export type { ImageTemplate, ParamValues, TemplateParam, ImageResolution } from "./template"; | ||
export { DefaultManifestName, renderTemplateToHtml, demoParamValues, paramLabel, validateParamValue, stringToParamValue, paramValueToString, FacebookOpenGraph, TwitterCard } from "./template"; | ||
export { loadRemoteTemplate, isAbsoluteUrl } from "./remote"; | ||
export type { ImageTemplate, ParamValue, ParamValues, TemplateParam, ImageResolution } from "./template"; | ||
export { ParamType } from "./template"; |
@@ -14,2 +14,3 @@ import Mustache from 'mustache'; | ||
ParamType["Choice"] = "choice"; | ||
ParamType["ObjectList"] = "objectList"; | ||
})(ParamType || (ParamType = {})); | ||
@@ -40,21 +41,32 @@ var FacebookOpenGraph = { | ||
*/ | ||
var validateParamValue = function (paramSpec, paramValue) { | ||
switch (paramSpec.type) { | ||
case (ParamType.String): | ||
return; | ||
case (ParamType.Color): | ||
// TODO: Check color format | ||
return; | ||
case (ParamType.ImageUrl): | ||
// TODO: Check basic format | ||
return; | ||
var validateParamValue = function (param, value) { | ||
stringToParamValue(param, value); | ||
}; | ||
var paramLabel = function (param) { return ( | ||
// See https://stackoverflow.com/questions/4149276/how-to-convert-camelcase-to-camel-case#answer-4149393 | ||
param.label || param.name.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) { return str.toUpperCase(); })); }; | ||
var stringToParamValue = function (param, value) { | ||
switch (param.type) { | ||
case (ParamType.Choice): | ||
if (paramSpec.values && !paramSpec.values.map(function (v) { return v.value; }).includes(paramValue)) { | ||
throw "Invalid value \"" + paramValue + "\" for " + paramSpec.label + ". Must be one of " + paramSpec.values.join(', '); | ||
if (param.values && !param.values.map(function (v) { return v.value; }).includes(value)) { | ||
throw "Invalid value \"" + value + "\" for " + param.label + ". Must be one of " + param.values.join(', '); | ||
} | ||
return value; | ||
case (ParamType.ObjectList): | ||
return JSON.parse(value); | ||
case (ParamType.String): | ||
case (ParamType.Color): // TODO: Check color format | ||
case (ParamType.ImageUrl): // TODO: Check basic format | ||
default: | ||
return value; | ||
} | ||
}; | ||
var paramLabel = function (param) { return ( | ||
// See https://stackoverflow.com/questions/4149276/how-to-convert-camelcase-to-camel-case#answer-4149393 | ||
param.label || param.name.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) { return str.toUpperCase(); })); }; | ||
var paramValueToString = function (param, value) { | ||
switch (param.type) { | ||
case (ParamType.ObjectList): | ||
return JSON.stringify(value); | ||
default: | ||
return value.toString(); | ||
} | ||
}; | ||
@@ -214,3 +226,3 @@ /*! ***************************************************************************** | ||
export { DefaultManifestName, FacebookOpenGraph, ParamType, TwitterCard, demoParamValues, loadRemoteTemplate, paramLabel, renderTemplateToHtml, validateParamValue }; | ||
export { DefaultManifestName, FacebookOpenGraph, ParamType, TwitterCard, demoParamValues, isAbsoluteUrl, loadRemoteTemplate, paramLabel, paramValueToString, renderTemplateToHtml, stringToParamValue, validateParamValue }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -23,2 +23,3 @@ 'use strict'; | ||
ParamType["Choice"] = "choice"; | ||
ParamType["ObjectList"] = "objectList"; | ||
})(exports.ParamType || (exports.ParamType = {})); | ||
@@ -49,21 +50,32 @@ var FacebookOpenGraph = { | ||
*/ | ||
var validateParamValue = function (paramSpec, paramValue) { | ||
switch (paramSpec.type) { | ||
case (exports.ParamType.String): | ||
return; | ||
case (exports.ParamType.Color): | ||
// TODO: Check color format | ||
return; | ||
case (exports.ParamType.ImageUrl): | ||
// TODO: Check basic format | ||
return; | ||
var validateParamValue = function (param, value) { | ||
stringToParamValue(param, value); | ||
}; | ||
var paramLabel = function (param) { return ( | ||
// See https://stackoverflow.com/questions/4149276/how-to-convert-camelcase-to-camel-case#answer-4149393 | ||
param.label || param.name.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) { return str.toUpperCase(); })); }; | ||
var stringToParamValue = function (param, value) { | ||
switch (param.type) { | ||
case (exports.ParamType.Choice): | ||
if (paramSpec.values && !paramSpec.values.map(function (v) { return v.value; }).includes(paramValue)) { | ||
throw "Invalid value \"" + paramValue + "\" for " + paramSpec.label + ". Must be one of " + paramSpec.values.join(', '); | ||
if (param.values && !param.values.map(function (v) { return v.value; }).includes(value)) { | ||
throw "Invalid value \"" + value + "\" for " + param.label + ". Must be one of " + param.values.join(', '); | ||
} | ||
return value; | ||
case (exports.ParamType.ObjectList): | ||
return JSON.parse(value); | ||
case (exports.ParamType.String): | ||
case (exports.ParamType.Color): // TODO: Check color format | ||
case (exports.ParamType.ImageUrl): // TODO: Check basic format | ||
default: | ||
return value; | ||
} | ||
}; | ||
var paramLabel = function (param) { return ( | ||
// See https://stackoverflow.com/questions/4149276/how-to-convert-camelcase-to-camel-case#answer-4149393 | ||
param.label || param.name.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) { return str.toUpperCase(); })); }; | ||
var paramValueToString = function (param, value) { | ||
switch (param.type) { | ||
case (exports.ParamType.ObjectList): | ||
return JSON.stringify(value); | ||
default: | ||
return value.toString(); | ||
} | ||
}; | ||
@@ -227,6 +239,9 @@ /*! ***************************************************************************** | ||
exports.demoParamValues = demoParamValues; | ||
exports.isAbsoluteUrl = isAbsoluteUrl; | ||
exports.loadRemoteTemplate = loadRemoteTemplate; | ||
exports.paramLabel = paramLabel; | ||
exports.paramValueToString = paramValueToString; | ||
exports.renderTemplateToHtml = renderTemplateToHtml; | ||
exports.stringToParamValue = stringToParamValue; | ||
exports.validateParamValue = validateParamValue; | ||
//# sourceMappingURL=index.js.map |
@@ -6,3 +6,4 @@ export declare const DefaultManifestName = "resoc.manifest.json"; | ||
ImageUrl = "imageUrl", | ||
Choice = "choice" | ||
Choice = "choice", | ||
ObjectList = "objectList" | ||
} | ||
@@ -13,2 +14,3 @@ export declare type ParamChoice = { | ||
}; | ||
export declare type ParamValue = string | Object[]; | ||
export declare type TemplateParam = { | ||
@@ -19,4 +21,4 @@ name: string; | ||
values?: ParamChoice[]; | ||
demoValue: string; | ||
defaultValue?: string; | ||
demoValue: ParamValue; | ||
defaultValue?: ParamValue; | ||
}; | ||
@@ -30,3 +32,3 @@ export interface ImageTemplate { | ||
export declare type ParamValues = { | ||
[name: string]: string; | ||
[name: string]: ParamValue; | ||
}; | ||
@@ -50,3 +52,5 @@ export declare type ImageResolution = { | ||
*/ | ||
export declare const validateParamValue: (paramSpec: TemplateParam, paramValue: string) => void; | ||
export declare const validateParamValue: (param: TemplateParam, value: string) => void; | ||
export declare const paramLabel: (param: TemplateParam) => string; | ||
export declare const stringToParamValue: (param: TemplateParam, value: string) => ParamValue; | ||
export declare const paramValueToString: (param: TemplateParam, value: ParamValue) => string; |
{ | ||
"name": "@resoc/core", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Resoc core utils", | ||
@@ -57,3 +57,3 @@ "main": "build/index.js", | ||
}, | ||
"gitHead": "25c7bcf1cc97afdaf172820fab056df954b80719" | ||
"gitHead": "c03dd64ee237cf366de30a56da5fa70a271c7389" | ||
} |
@@ -7,2 +7,4 @@ export { | ||
validateParamValue, | ||
stringToParamValue, | ||
paramValueToString, | ||
FacebookOpenGraph, | ||
@@ -12,6 +14,7 @@ TwitterCard | ||
export { loadRemoteTemplate } from "./remote"; | ||
export { loadRemoteTemplate, isAbsoluteUrl } from "./remote"; | ||
export type { | ||
ImageTemplate, | ||
ParamValue, | ||
ParamValues, | ||
@@ -18,0 +21,0 @@ TemplateParam, |
@@ -1,2 +0,2 @@ | ||
import { assignResolutionToParamerters, paramLabel, ParamType, parseTemplateManifestParams, renderRawTemplate, validateParamValue } from './template' | ||
import { assignResolutionToParamerters, demoParamValues, paramLabel, ParamType, paramValueToString, parseTemplateManifestParams, renderRawTemplate, stringToParamValue, validateParamValue } from './template' | ||
@@ -48,2 +48,10 @@ test('renderRawTemplate', () => { | ||
}, 'Wakanda')).toThrowError(); | ||
validateParamValue({ | ||
name: 'obj', type: ParamType.ObjectList, demoValue: '[ { "a": "1", "b": "2"} ]' | ||
}, '[ { "x": "1", "y": "2"}, { "a": "98", "b": "99"} ]'); | ||
expect(() => validateParamValue({ | ||
name: 'obj', type: ParamType.ObjectList, demoValue: '[ { "a": "1", "b": "2"} ]' | ||
}, 'Not JSON...')).toThrowError(); | ||
}); | ||
@@ -73,1 +81,52 @@ | ||
}); | ||
test('stringToParamValue', () => { | ||
expect(stringToParamValue({ | ||
name: 'someText', type: ParamType.String, demoValue: 'Foo' | ||
}, 'Lorem ipsum')).toEqual('Lorem ipsum'); | ||
expect(stringToParamValue({ | ||
name: 'theColor', type: ParamType.Color, demoValue: '#456789' | ||
}, '#abcdef')).toEqual('#abcdef'); | ||
expect(stringToParamValue({ | ||
name: 'country', type: ParamType.Choice, values: [{ value: 'Spain' }, { value: 'France' }], demoValue: 'Spain' | ||
}, 'France')).toEqual('France'); | ||
expect(() => stringToParamValue({ | ||
name: 'country', type: ParamType.Choice, values: [{ value: 'Spain' }, { value: 'France' }], demoValue: 'Spain' | ||
}, 'Wakanda')).toThrowError(); | ||
expect(stringToParamValue({ | ||
name: 'obj', type: ParamType.ObjectList, demoValue: '[ { "a": "1", "b": "2"} ]' | ||
}, '[ { "x": "1", "y": "2"}, { "a": "98", "b": "99"} ]')).toEqual([ | ||
{ x: "1", y: "2"}, { a: "98", b: "99"} | ||
]); | ||
expect(() => stringToParamValue({ | ||
name: 'obj', type: ParamType.ObjectList, demoValue: '[ { "a": "1", "b": "2"} ]' | ||
}, 'Not JSON...')).toThrowError(); | ||
}); | ||
test('paramValueToString', () => { | ||
expect(paramValueToString({ | ||
name: 'someText', type: ParamType.String, demoValue: 'Foo' | ||
}, 'Lorem ipsum')).toEqual('Lorem ipsum'); | ||
expect(paramValueToString({ | ||
name: 'obj', type: ParamType.ObjectList, demoValue: '[ { "a": "1", "b": "2"} ]' | ||
}, [ { x: "1", y: "2"}, { a: "98", b: "99"} ])).toEqual( | ||
'[{"x":"1","y":"2"},{"a":"98","b":"99"}]' | ||
); | ||
}); | ||
test('demoParamValues', () => { | ||
expect(demoParamValues([ | ||
{ name: 'obj', type: ParamType.ObjectList, demoValue: [ { a: "1", b: "2"} ] }, | ||
{ name: 'txt', type: ParamType.String, demoValue: 'Foo' } | ||
])).toEqual({ | ||
txt: 'Foo', | ||
obj: [ { a: "1", b: "2"} ] | ||
}); | ||
}); |
@@ -48,3 +48,4 @@ import axios from 'axios'; | ||
ImageUrl = 'imageUrl', | ||
Choice = 'choice' | ||
Choice = 'choice', | ||
ObjectList = 'objectList' | ||
}; | ||
@@ -57,2 +58,4 @@ | ||
export type ParamValue = string | Object[]; | ||
export type TemplateParam = { | ||
@@ -63,4 +66,4 @@ name: string; | ||
values?: ParamChoice[]; | ||
demoValue: string; | ||
defaultValue?: string; | ||
demoValue: ParamValue; | ||
defaultValue?: ParamValue; | ||
}; | ||
@@ -73,3 +76,3 @@ | ||
export type ParamValues = { [ name: string ]: string }; | ||
export type ParamValues = { [ name: string ]: ParamValue }; | ||
@@ -130,17 +133,4 @@ export type ImageResolution = { | ||
*/ | ||
export const validateParamValue = (paramSpec: TemplateParam, paramValue: string) => { | ||
switch(paramSpec.type) { | ||
case(ParamType.String): | ||
return; | ||
case(ParamType.Color): | ||
// TODO: Check color format | ||
return; | ||
case(ParamType.ImageUrl): | ||
// TODO: Check basic format | ||
return; | ||
case(ParamType.Choice): | ||
if (paramSpec.values && !paramSpec.values.map(v => v.value).includes(paramValue)) { | ||
throw `Invalid value "${paramValue}" for ${paramSpec.label }. Must be one of ${paramSpec.values.join(', ')}`; | ||
} | ||
} | ||
export const validateParamValue = (param: TemplateParam, value: string) => { | ||
stringToParamValue(param, value); | ||
}; | ||
@@ -152,1 +142,30 @@ | ||
); | ||
export const stringToParamValue = (param: TemplateParam, value: string): ParamValue => { | ||
switch(param.type) { | ||
case(ParamType.Choice): | ||
if (param.values && !param.values.map(v => v.value).includes(value)) { | ||
throw `Invalid value "${value}" for ${param.label }. Must be one of ${param.values.join(', ')}`; | ||
} | ||
return value; | ||
case(ParamType.ObjectList): | ||
return JSON.parse(value); | ||
case(ParamType.String): | ||
case(ParamType.Color): // TODO: Check color format | ||
case(ParamType.ImageUrl): // TODO: Check basic format | ||
default: | ||
return value; | ||
} | ||
}; | ||
export const paramValueToString = (param: TemplateParam, value: ParamValue): string => { | ||
switch(param.type) { | ||
case(ParamType.ObjectList): | ||
return JSON.stringify(value); | ||
default: | ||
return value.toString(); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
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
41591
869