Skip to content

Commit

Permalink
Merge pull request #91 from ayush2k/bugfix/fallback
Browse files Browse the repository at this point in the history
adds function to fallback gracefully
  • Loading branch information
Snugug authored Aug 12, 2016
2 parents 61fc6a3 + e0a629a commit 8e485a2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/content-types/only.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
const merge = require('deepmerge');
const utils = require('../utils');

const only = (type, config) => {
const only = (type, configuration) => {
return new Promise((resolve) => {
const mergedType = type;
const config = configuration;
const configured = Object.keys(config);
const attrs = mergedType.attributes.map(attribute => {
const attr = attribute;
Expand All @@ -23,6 +24,11 @@ const only = (type, config) => {
return attr;
}

// check if repeatable but not an array
if (attr.hasOwnProperty('repeatable') && typeof attr.repeatable === 'object' && !Array.isArray(config[attr.id])) {
config[attr.id] = [config[attr.id]];
}

// Flattens multiple instances and updates ids and names
if (Array.isArray(config[attr.id])) {
attr.inputs = config[attr.id].map((data, index) => {
Expand Down
5 changes: 5 additions & 0 deletions lib/form/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ const addRequired = (html, input, index) => {
// THIS input's label
const label = render.match(regexLabel);

// checks if label is not found
if (label === null || !Array.isArray(label) || label.length < 2) {
return;
}

// regex to get first class attributes from the captured label
const regexClass = new RegExp(`.*(${stringClass})`);

Expand Down
6 changes: 4 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.singleItem = (key, value, arr) => {
};

/**
* Pop the last element after splitting the string
* Pop the last element if length > 1 after splitting the string
*
* @param {string} input - Input string to be split
* @param {string} splitter - string to split against
Expand All @@ -46,7 +46,9 @@ exports.singleItem = (key, value, arr) => {
*/
exports.splitPop = (input, splitter) => {
let blocks = input.split(splitter);
blocks.pop();
if (blocks.length > 1) {
blocks.pop();
}
blocks = blocks.join(splitter);

return blocks;
Expand Down
3 changes: 3 additions & 0 deletions tests/content-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ test('Content Types', t => {
source: { value: 'baz2' },
},
],
'something-new-other': {
text: { value: 'bar' },
},
}).then(result => {
t.deepEqual(result, barExpected, 'Only method works!');
t.pass();
Expand Down
31 changes: 31 additions & 0 deletions tests/fixtures/objects/bar-expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,37 @@ const barExpected = {
type: 'quote',
validation: 'validation',
},
{
description: 'I am the Bar Content Type Config text field repeatable',
html: '<label for="{{text.id}}">{{text.label}}</label><input type="{{text.type}}" id="{{text.id}}" name="{{text.name}}" value="{{text.value}}" placeholder="{{text.placeholder}}" />',
id: 'something-new',
repeatable: {
min: 1,
max: 9007199254740991,
},
inputs: [
{
text: {
id: '5ba58361-87b7-4e79-8ea0-e54635ad23bb--0',
label: 'SOme New THING',
name: 'something-new--text--0',
placeholder: 'Text Goes Here',
settings: {
empty: true,
},
type: 'text',
validation: {
function: 'textValidation',
on: 'blur',
},
value: 'foo',
},
},
],
name: 'SOme New THING',
type: 'text',
validation: 'validation',
},
],
};

Expand Down
30 changes: 30 additions & 0 deletions tests/fixtures/objects/bar-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ const barInput = {
type: 'quote',
validation: 'validation',
},
{
description: 'I am the Bar Content Type Config text field repeatable',
html: '<label for="{{text.id}}">{{text.label}}</label><input type="{{text.type}}" id="{{text.id}}" name="{{text.name}}" value="{{text.value}}" placeholder="{{text.placeholder}}" />',
id: 'something-new',
repeatable: {
min: 1,
max: 9007199254740991,
},
inputs: [
{
text: {
id: '5ba58361-87b7-4e79-8ea0-e54635ad23bb--0',
label: 'SOme New THING',
name: 'something-new--text--0',
placeholder: 'Text Goes Here',
settings: {
empty: true,
},
type: 'text',
validation: {
function: 'textValidation',
on: 'blur',
},
},
},
],
name: 'SOme New THING',
type: 'text',
validation: 'validation',
},
],
};

Expand Down

0 comments on commit 8e485a2

Please sign in to comment.