Skip to content

Commit 95a60dc

Browse files
authored
refactor: remove ramda from textkit package (diegomura#1830)
1 parent 39297b6 commit 95a60dc

File tree

13 files changed

+216
-0
lines changed

13 files changed

+216
-0
lines changed

adjust.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const adjust = (index, fn, collection) => {
2+
if (index >= 0 && index >= collection.length) return collection;
3+
if (index < 0 && Math.abs(index) > collection.length) return collection;
4+
5+
const i = index < 0 ? collection.length + index : index;
6+
7+
return Object.assign([], collection, { [i]: fn(collection[i]) });
8+
};
9+
10+
export default adjust;

adjust.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import adjust from './adjust';
2+
3+
const add = v => v + 1;
4+
5+
describe('adjust', () => {
6+
test('applies the given function to the value at the given index of the supplied array', () => {
7+
const expected = [0, 1, 3, 3];
8+
const result = adjust(2, add, [0, 1, 2, 3]);
9+
10+
expect(result).toEqual(expected);
11+
});
12+
13+
test('offsets negative indexes from the end of the array', () => {
14+
const expected = [0, 2, 2, 3];
15+
const result = adjust(-3, add, [0, 1, 2, 3]);
16+
17+
expect(result).toEqual(expected);
18+
});
19+
20+
test('returns the original array if the supplied index is out of bounds', () => {
21+
const expected = [0, 1, 2, 3];
22+
23+
expect(adjust(4, add, expected)).toBe(expected);
24+
expect(adjust(-5, add, expected)).toBe(expected);
25+
});
26+
27+
test('does not mutate the original array', () => {
28+
const expected = [0, 1, 2, 3];
29+
adjust(2, add, expected);
30+
expect([0, 1, 2, 3]).toEqual(expected);
31+
});
32+
});

dropLast.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const dropLast = array => array.slice(0, array.length - 1);
2+
3+
export default dropLast;

dropLast.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import dropLast from './dropLast';
2+
3+
describe('dropLast', () => {
4+
test('skips the last element from a list, returning the remainder', () => {
5+
expect(dropLast(['a', 'b', 'c', 'd'])).toEqual(['a', 'b', 'c']);
6+
});
7+
8+
test('returns an empty array if passed 1 element array', () => {
9+
expect(dropLast(['a'])).toEqual([]);
10+
});
11+
12+
test('returns an empty array if passed empty array', () => {
13+
expect(dropLast([])).toEqual([]);
14+
});
15+
16+
test('can operate on strings', () => {
17+
expect(dropLast('react-pdf')).toEqual('react-pd');
18+
});
19+
});

evolve.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const evolve = (transformations, object) => {
2+
const result = object instanceof Array ? [] : {};
3+
const keys = Object.keys(object);
4+
5+
for (let i = 0; i < keys.length; i += 1) {
6+
const key = keys[i];
7+
const transformation = transformations[key];
8+
const type = typeof transformation;
9+
10+
if (type === 'function') {
11+
result[key] = transformation(object[key]);
12+
} else if (transformation && type === 'object') {
13+
result[key] = evolve(transformation, object[key]);
14+
} else result[key] = object[key];
15+
}
16+
17+
return result;
18+
};
19+
20+
export default evolve;

get.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import isNil from './isNil';
2+
import castArray from './castArray';
3+
4+
const get = (target, path, defaultValue) => {
5+
if (isNil(target)) return defaultValue;
6+
7+
const _path = castArray(path);
8+
9+
let result = target;
10+
11+
for (let i = 0; i < _path.length; i += 1) {
12+
if (isNil(result)) return undefined;
13+
14+
result = result[_path[i]];
15+
}
16+
17+
return isNil(result) ? defaultValue : result;
18+
};
19+
20+
export default get;

get.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import get from './get';
2+
3+
describe('get', () => {
4+
const deepObject = {
5+
a: { b: { c: 'c' } },
6+
falseVal: false,
7+
nullVal: null,
8+
undefinedVal: undefined,
9+
arrayVal: ['arr'],
10+
};
11+
12+
test('takes a path and an object and returns the value at the path or the default value', () => {
13+
const obj = {
14+
a: {
15+
b: {
16+
c: 100,
17+
d: 200,
18+
},
19+
e: {
20+
f: [100, 101, 102],
21+
g: 'G',
22+
},
23+
h: 'H',
24+
},
25+
i: 'I',
26+
j: ['J'],
27+
};
28+
29+
expect(get(obj, ['a', 'b', 'c'], 'Unknown')).toBe(100);
30+
expect(get(obj, [], 'Unknown')).toBe(obj);
31+
expect(get(obj, ['a', 'e', 'f', 1], 'Unknown')).toBe(101);
32+
expect(get(obj, ['j', 0], 'Unknown')).toBe('J');
33+
expect(get(obj, ['j', 1], 'Unknown')).toBe('Unknown');
34+
expect(get(null, ['a', 'b', 'c'], 'Unknown')).toBe('Unknown');
35+
});
36+
37+
test("gets a deep property's value from objects", () => {
38+
expect(get(deepObject, ['a', 'b', 'c'], 'Unknown')).toBe('c');
39+
expect(get(deepObject, ['a'], 'Unknown')).toBe(deepObject.a);
40+
});
41+
42+
test('returns the default value for items not found', () => {
43+
expect(get(deepObject, ['a', 'b', 'foo'], 'Unknown')).toBe('Unknown');
44+
expect(get(deepObject, ['bar'], 'Unknown')).toBe('Unknown');
45+
});
46+
47+
test('returns the default value for null/undefined', () => {
48+
expect(get(null, ['toString'], 'Unknown')).toBe('Unknown');
49+
expect(get(undefined, ['toString'], 'Unknown')).toBe('Unknown');
50+
});
51+
52+
test('works with falsy items', () => {
53+
expect(get(false, ['toString'], 'Unknown')).toBe(
54+
Boolean.prototype.toString,
55+
);
56+
});
57+
});

isNil.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const isNil = value => value === null || value === undefined;
2+
3+
export default isNil;

isNil.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable no-void */
2+
3+
import isNil from './isNil';
4+
5+
describe('isNil', () => {
6+
test('tests a value for `null` or `undefined`', () => {
7+
expect(isNil(void 0)).toBe(true);
8+
expect(isNil(null)).toBe(true);
9+
expect(isNil([])).toBe(false);
10+
expect(isNil({})).toBe(false);
11+
expect(isNil(0)).toBe(false);
12+
expect(isNil('')).toBe(false);
13+
});
14+
});

last.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const last = value => {
2+
return value === '' ? '' : value[value.length - 1];
3+
};
4+
5+
export default last;

0 commit comments

Comments
 (0)