Skip to content

Commit

Permalink
Merge 0c9716c into 4e54cbe
Browse files Browse the repository at this point in the history
  • Loading branch information
leofavre committed Dec 18, 2018
2 parents 4e54cbe + 0c9716c commit 83d0550
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 10 deletions.
20 changes: 19 additions & 1 deletion packages/sling-helpers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/sling-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"jsnext:main": "dist/es/es6/index.js",
"author": "Stone Pagamentos",
"dependencies": {
"moment": "^2.22.2"
"is-plain-object": "^2.0.4",
"moment": "^2.22.2",
"timm": "^1.6.1"
}
}
19 changes: 14 additions & 5 deletions packages/sling-helpers/src/dom/domHelper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export { makeRequest } from './makeRequest.js';

export const waitUntilEvent = (name, $el = document) =>
export const waitUntilEvent = (name, domEl = document) =>
new Promise((resolve) => {
const handleEvent = () => {
$el.removeEventListener(name, handleEvent);
domEl.removeEventListener(name, handleEvent);
resolve();
};
$el.addEventListener(name, handleEvent);
domEl.addEventListener(name, handleEvent);
});

export const waitUntilTagIsAppended = (name, $el) =>
export const waitUntilTagIsAppended = (name, domEl) =>
new Promise((resolve) => {
let observer;

Expand All @@ -29,7 +29,7 @@ export const waitUntilTagIsAppended = (name, $el) =>

observer = new MutationObserver(handleMutations);

observer.observe($el, {
observer.observe(domEl, {
childList: true,
subtree: true,
});
Expand All @@ -40,3 +40,12 @@ export const registerComponent = (name, DefinitionClass) => {
window.customElements.define(name, DefinitionClass);
}
};

export const setAttr = (domEl, attrName, attrValue) => {
if (!attrValue && attrValue !== 0 && attrValue !== '') {
domEl.removeAttribute(attrName);
} else {
const parsedAttribute = (attrValue === true) ? '' : attrValue;
domEl.setAttribute(attrName, parsedAttribute);
}
};
9 changes: 9 additions & 0 deletions packages/sling-helpers/src/dom/domHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
waitUntilEvent,
waitUntilTagIsAppended,
registerComponent,
setAttr,
} from './domHelper.js';

describe('waitUntilEvent', () => {
Expand Down Expand Up @@ -58,3 +59,11 @@ describe('registerComponent', () => {
expect(window.customElements.get('test-component')).to.equal(testComponent);
});
});

describe('setAttr', () => {
it('Should set attributes into an element.', () => {
const $div = document.createElement('div');
setAttr($div, 'testName', 'testValue');
expect($div.getAttribute('testName')).to.equal('testValue');
});
});
74 changes: 74 additions & 0 deletions packages/sling-helpers/src/global/globalHelper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'moment/locale/pt-br';
import moment from 'moment/moment';
import { setIn as timmSetIn, getIn as timmGetIn } from 'timm';
import isPlainObject from 'is-plain-object';

moment.locale('pt-BR');

export { omit, mergeDeep } from 'timm';

export const isString = arg =>
typeof arg === 'string' || arg instanceof String;

Expand Down Expand Up @@ -90,3 +94,73 @@ export const mapByKey = (collection = [], key, value) =>
acc[actual[key]] = actual[value];
return acc;
}, {});

export const sleep = ms =>
new Promise(resolve => setTimeout(resolve, ms));

const toPath = str => str
.replace(/\]$/g, '')
.replace(/(\[|\])/g, '.')
.replace(/\.{2,}/g, '.')
.split('.')
.map(key => (!Number.isNaN(Number(key)) ? Number(key) : key));

export const setIn = (obj, path, value) => {
const normalizedPath = toPath(path);
return timmSetIn(obj, normalizedPath, value);
};

export const getIn = (obj, path) => {
const normalizedPath = toPath(path);
return timmGetIn(obj, normalizedPath);
};

export const isDeeplyEmpty = items => (items == null) ||
Object
.values(items)
.map((value) => {
if (isPlainObject(value)) {
return isDeeplyEmpty(value);
}

if (Array.isArray(value)) {
return value.every(isDeeplyEmpty);
}

return value == null;
})
.filter(hasError => !hasError)
.length === 0;

export const mergeUnique = (...arrays) => [...new Set(arrays
.reduce((result, arr) => [...result, ...arr], []))];

export const compose = (...fns) =>
fns.reduceRight((prevFn, nextFn) =>
(...args) => nextFn(prevFn(...args)), value => value);

export const flatten = (obj) => {
const step = (currentKey, into, target = {}) => {
Object
.keys(into)
.forEach((key) => {
let newKey = key;
const newVal = into[key];

if (currentKey.length > 0) {
const endKey = Number.isNaN(Number(key)) ? key : `[${key}]`;
newKey = `${currentKey}.${endKey}`.replace('.[', '[');
}

if (typeof newVal === 'object') {
step(newKey, newVal, target);
} else {
target[newKey] = newVal;
}
});
};

const result = {};
step('', obj, result);
return result;
};
55 changes: 55 additions & 0 deletions packages/sling-helpers/src/global/globalHelper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import {
groupByDeep,
mapByKey,
getDateRangeArray,
isDeeplyEmpty,
flatten,
mergeUnique,
setIn,
getIn,
sleep,
} from './globalHelper.js';

const { expect } = chai;
Expand Down Expand Up @@ -555,3 +561,52 @@ describe('mapByKey', () => {
});
});
});

describe('isDeeplyEmpty', () => {
it('Should return true for empty properties', () => {
const test = { a: null, b: undefined, c: [], d: [null, undefined] };
chai.assert.isTrue(isDeeplyEmpty(test));
});

it('Should check if have at least one property', () => {
const test = { a: null, b: undefined, c: [], d: ['testString', undefined] };
chai.assert.isFalse(isDeeplyEmpty(test));
});
});

describe('flatten', () => {
it('Should return the expected flat result', () => {
const test = { a: ['art', 'ant'], b: { basic: true } };
const expectResult = { 'a[0]': 'art', 'a[1]': 'ant', 'b.basic': true };
expect(flatten(test)).to.eql(expectResult);
});
});

describe('mergeUnique', () => {
it('Should return the expected merged result', () => {
expect(mergeUnique([1, 2], [2, 3, 4], [3, 4, 5])).to.eql([1, 2, 3, 4, 5]);
});
});

describe('setIn', () => {
it('Should set value in the right path', () => {
const object = { a: [{ b: { c: 3 } }] };
const testResult = setIn(object, 'a[0].b.c', 4);
expect(testResult.a[0].b.c).to.eql(4);
});
});

describe('getIn', () => {
it('Should get value in the right path', () => {
const object = { a: [{ b: { c: 3 } }] };
expect(getIn(object, 'a[0].b.c')).to.eql(3);
});
});

describe('sleep', () => {
it(
'Should return a promise that is resolved'
+ ' after the given delay', (done) => {
sleep(100).then(done);
});
});
2 changes: 1 addition & 1 deletion packages/sling-web-component-calendar/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sling-web-component-input/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sling-web-component-table/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83d0550

Please sign in to comment.