Skip to content

Commit ecc066f

Browse files
authored
refactor: remove ramda from layout package (diegomura#1827)
refactor: remove rambda from layout package
1 parent 95a60dc commit ecc066f

11 files changed

+189
-3
lines changed

asyncCompose.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable no-await-in-loop */
2+
3+
import reverse from './reverse';
4+
5+
/**
6+
* Performs right-to-left function composition with async functions support
7+
*
8+
* @param {...any} functions
9+
*/
10+
const asyncCompose = (...fns) => async (value, ...args) => {
11+
let result = value;
12+
const reversedFns = reverse(fns);
13+
14+
for (let i = 0; i < reversedFns.length; i += 1) {
15+
const fn = reversedFns[i];
16+
result = await fn(result, ...args);
17+
}
18+
19+
return result;
20+
};
21+
22+
export default asyncCompose;

capitalize.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Capitalize first letter of each word
3+
*
4+
* @param {String} string
5+
* @returns {String} capitalized string
6+
*/
7+
const capitalize = value => {
8+
if (!value) return value;
9+
return value.replace(/(^|\s)\S/g, l => l.toUpperCase());
10+
};
11+
12+
export default capitalize;

capitalize.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import capitalize from './capitalize';
2+
3+
describe('capitalize', () => {
4+
test('should return undefined for undefined', () => {
5+
expect(capitalize(undefined)).toBe(undefined);
6+
});
7+
8+
test('should return null for null', () => {
9+
expect(capitalize(null)).toBe(null);
10+
});
11+
12+
test('should return empty string when empty string', () => {
13+
expect(capitalize('')).toBe('');
14+
});
15+
16+
test('should return single word string capitalized', () => {
17+
expect(capitalize('reactpdf')).toBe('Reactpdf');
18+
});
19+
20+
test('should return multiple word string capitalized', () => {
21+
expect(capitalize('lorem ipsum')).toBe('Lorem Ipsum');
22+
});
23+
});

mapValues.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const mapValues = (object, fn) => {
2+
const entries = Object.entries(object);
3+
4+
return entries.reduce((acc, [key, value], index) => {
5+
acc[key] = fn(value, key, index);
6+
return acc;
7+
}, {});
8+
};
9+
10+
export default mapValues;

matchPercent.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const isPercent = value => /((-)?\d+\.?\d*)%/g.exec(value);
2+
3+
/**
4+
* Get percentage value of input
5+
*
6+
* @param {String} value
7+
* @returns {Object} percent value (if matches)
8+
*/
9+
const matchPercent = value => {
10+
const match = isPercent(value);
11+
12+
if (match) {
13+
const f = parseFloat(match[1], 10);
14+
const percent = f / 100;
15+
16+
return { percent, value: f };
17+
}
18+
19+
return null;
20+
};
21+
22+
export default matchPercent;

matchPercent.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import matchPercent from './matchPercent';
2+
3+
describe('match percent', () => {
4+
test('should return null for null input', () => {
5+
expect(matchPercent(null)).toBeNull();
6+
});
7+
8+
test('should return null for numeric inputs', () => {
9+
expect(matchPercent(40)).toBeNull();
10+
});
11+
12+
test('should return null for wrong string inputs', () => {
13+
expect(matchPercent('hey%')).toBeNull();
14+
});
15+
16+
test('should return value for positive integer percents', () => {
17+
const match = matchPercent('35%');
18+
19+
expect(match.value).toBe(35);
20+
expect(match.percent).toBe(0.35);
21+
});
22+
23+
test('should return value for positive real percents', () => {
24+
const match = matchPercent('35.5%');
25+
26+
expect(match.value).toBe(35.5);
27+
expect(match.percent).toBe(0.355);
28+
});
29+
30+
test('should return value for negative integer percents', () => {
31+
const match = matchPercent('-35%');
32+
33+
expect(match.value).toBe(-35);
34+
expect(match.percent).toBe(-0.35);
35+
});
36+
37+
test('should return value for negative real percents', () => {
38+
const match = matchPercent('-35.5%');
39+
40+
expect(match.value).toBe(-35.5);
41+
expect(match.percent).toBe(-0.355);
42+
});
43+
});

omit.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
const omit = (key, object) => {
1+
import castArray from './castArray';
2+
3+
const omit = (keys, object) => {
4+
const _keys = castArray(keys);
5+
26
const copy = Object.assign({}, object);
37

4-
delete copy[key];
8+
_keys.forEach(key => {
9+
delete copy[key];
10+
});
511

612
return copy;
713
};

omit.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import omit from './omit';
33
describe('omit', () => {
44
const obj = { a: 1, b: 2, c: 3 };
55

6-
test('copies an object omitting the listed properties', () => {
6+
test('copies an object omitting the listed property', () => {
77
expect(omit('a', obj), { b: 2, c: 3 });
88
});
9+
10+
test('copies an object omitting the listed properties', () => {
11+
expect(omit(['a', 'c'], obj), { c: 3 });
12+
});
913
});

pick.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const pick = (keys, obj) => {
2+
const result = {};
3+
4+
for (let i = 0; i < keys.length; i += 1) {
5+
const key = keys[i];
6+
7+
if (key in obj) result[key] = obj[key];
8+
}
9+
10+
return result;
11+
};
12+
13+
export default pick;

upperFirst.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Capitalize first letter of string
3+
*
4+
* @param {String} string
5+
* @returns {String} capitalized string
6+
*/
7+
const upperFirst = value => {
8+
if (!value) return value;
9+
return value.charAt(0).toUpperCase() + value.slice(1);
10+
};
11+
12+
export default upperFirst;

0 commit comments

Comments
 (0)