Skip to content

Commit

Permalink
feat(filters): updated implementations, added test
Browse files Browse the repository at this point in the history
- dividedBy, round, split, stripTags, urlize
  • Loading branch information
noahlange committed Oct 22, 2017
1 parent 0092baa commit c811311
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 30 deletions.
8 changes: 4 additions & 4 deletions packages/superjucks-runtime/src/filters/dividedBy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function dividedBy(value: number, toDivideBy: number) {
const isInteger = Number.isInteger(toDivideBy);
const divided = value / toDivideBy;
return isInteger ? Math.floor(divided) : divided;
export default function dividedBy(value: number | string, toDivideBy: number | string) {
const one = typeof value === 'string' ? parseFloat(value) : value;
const two = typeof toDivideBy === 'string' ? parseFloat(toDivideBy) : toDivideBy;
return one / two;
}
2 changes: 2 additions & 0 deletions packages/superjucks-runtime/src/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import compact from './compact';
import date from './date';
import defaults from './default';
import dictSort from './dictSort';
import dividedBy from './dividedBy';
import dump from './dump';
import escape from './escape';
import first from './first';
Expand Down Expand Up @@ -73,6 +74,7 @@ export {
defaults as default,
defaults as d,
dictSort,
dividedBy,
dump,
escape,
escape as e,
Expand Down
4 changes: 1 addition & 3 deletions packages/superjucks-runtime/src/filters/round.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export default function round(n: number | string) {
if (typeof n === 'string') {
n = parseFloat(n);
}
n = typeof n === 'string' ? parseFloat(n) : n;
return Math.round(n);
}
4 changes: 2 additions & 2 deletions packages/superjucks-runtime/src/filters/split.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { entries } from '../runtime';

export default function split(val: string | any[] | { [key: string]: any } | Map<any, any> | Set<any>) {
return typeof val === 'string' ? val.split('') : [ ...entries(val) ];
export default function split(val: string) {
return val.split('');
}
1 change: 0 additions & 1 deletion packages/superjucks-runtime/src/filters/stripTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { copySafeness } from '../runtime';
import trim from './trim';

export default function striptags(input: string, preserveLinebreaks?: boolean) {
input = input || '';
preserveLinebreaks = preserveLinebreaks || false;
const tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>|<!--[\s\S]*?-->/gi;
const trimmedInput = trim(input.replace(tags, ''));
Expand Down
10 changes: 3 additions & 7 deletions packages/superjucks-runtime/src/filters/urlize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export default function urlize(str: string, length?: number, noFollow?: boolean) {
if (isNaN(length)) {
length = Infinity;
}

export default function urlize(str: string, length: number = Infinity, noFollow?: boolean) {
const noFollowAttr = (noFollow === true ? ' rel="nofollow"' : '');

// For the jinja regexp, see
Expand All @@ -27,7 +23,7 @@ export default function urlize(str: string, length?: number, noFollow?: boolean)

// url that starts with www.
if (wwwRE.test(possibleUrl)) {
return '<a href="http://' + possibleUrl + '"' + noFollowAttr + '>' + possibleUrl.substr(0, length) + '</a>';
return '<a href="https://' + possibleUrl + '"' + noFollowAttr + '>' + possibleUrl.substr(0, length) + '</a>';
}

// an email address of the form username@domain.tld
Expand All @@ -37,7 +33,7 @@ export default function urlize(str: string, length?: number, noFollow?: boolean)

// url that ends in .com, .org or .net that is not an email address
if (tldRE.test(possibleUrl)) {
return '<a href="http://' + possibleUrl + '"' + noFollowAttr + '>' + possibleUrl.substr(0, length) + '</a>';
return '<a href="https://' + possibleUrl + '"' + noFollowAttr + '>' + possibleUrl.substr(0, length) + '</a>';
}

return word;
Expand Down
76 changes: 63 additions & 13 deletions packages/superjucks-runtime/src/tests/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ test('ceil should provide the ceil of an number', async t => {
test('center should center a string in another string', async t => {
t.is(filters.center('ZARGOTHRAX', 4), 'ZARG');
t.is(filters.center('ZARGOTHRAX', 32), ' ZARGOTHRAX ');
t.is(filters.center(), ' ');
t.is(
filters.center(),
' '
);
});

test('chunk should chunk an array into n-lengthed arrays', async t => {
Expand All @@ -42,7 +45,7 @@ test('chunk should chunk an array into n-lengthed arrays', async t => {
.join('\n');

const chunked2 = filters
.chunk([ 1, 2, 3, 4 ], 2)
.chunk([1, 2, 3, 4], 2)
.map(a => a.join(''))
.join('\n');

Expand All @@ -62,6 +65,7 @@ test('date should format a date object into a human-readable string', async t =>
});

test('default should provide a default value for a lookup', async t => {
t.is(filters.default('hulk', 'smash'), 'hulk');
t.is(filters.default(undefined, 'smash'), 'smash');
});

Expand Down Expand Up @@ -159,11 +163,11 @@ test('group by should group an object by an attribute', t => {
});

test('join should join an array on a delimeter', async t => {
t.is(filters.join([1, 2, 3, 4, 5], ''), '12345');
t.is(filters.join([1, 2, 3, 4, 5]), '12345');
});

test('join should join an array of objects on a delimeter via a key', async t => {
t.is(filters.join([{ foo: 'bar' }, { foo: 'baz' }], '', 'foo'), 'barbaz');
t.is(filters.join([{ foo: 'bar' }, { foo: 'baz' }], '|', 'foo'), 'bar|baz');
});

test('last should return the last item in an iterable', async t => {
Expand Down Expand Up @@ -200,6 +204,17 @@ test('minus should subtract one value from another', async t => {
t.is(filters.minus(5.0, 2), 3.0);
});

test('dividedBy should divide one value by another', async t => {
t.is(filters.dividedBy('5.0', 2), 2.5);
t.is(filters.dividedBy(5, '2'), 2.5);
t.is(filters.dividedBy('5', 2.0), 2.5);
t.is(filters.dividedBy(5.0, '2.0'), 2.5);
});

test('times should multiply one value by another', async t => {
t.is(filters.times(5.0, 2), 10.0);
});

test('modulo should find the remainder of a division operation', async t => {
t.is(filters.modulo(5, 2), 1);
});
Expand Down Expand Up @@ -247,6 +262,7 @@ test('remove should remove all instances of one string from another', async t =>

test('round should round a number', async t => {
t.is(filters.round(4.5), 5);
t.is(filters.round('4.5'), 5);
});

test('removeFirst should remove the first instance of one string from another', async t => {
Expand Down Expand Up @@ -282,7 +298,7 @@ test('sum should sum the properties of an object by key', t => {
'123'
);

t.is(filters.sum([ 1, 2, 3, 4, 5 ]), 15);
t.is(filters.sum([1, 2, 3, 4, 5]), 15);
});

test('toCase should change the case of a string', async t => {
Expand All @@ -309,21 +325,40 @@ test('truncate words should truncate x words of arbitrary length to n words', as
filters.truncateWords('The cat came back the very next day', 4, '...'),
'The cat came back...'
);
t.is(
filters.truncateWords('Oh hi, Mark!', 4), 'Oh hi, Mark!'
);
t.is(filters.truncateWords('Oh hi, Mark!', 4), 'Oh hi, Mark!');
});

test('stirp tags should stripe the tags from an html string', t => {
test('strip tags should stripe the tags from an html string', t => {
const str = `<span>I'm back in the saddle again!</span>`;
t.is(filters.stripTags(str, false), `I'm back in the saddle again!`);
});

test('strip tags should stripe the tags from an html string, leaving line breaks intact', t => {
const str = '<span>I\'m back in the\nsaddle again!</span>';
t.is(filters.stripTags(str, true), `I'm back in the\nsaddle again!`);
});

test('urlize is not my code and does magical things', t => {
t.is(
filters.urlize('foo http://www.example.com bar', Infinity, true),
`foo <a href="http://www.example.com" rel="nofollow">http://www.example.com</a> bar`
);
t.is(
filters.urlize('foo http://www.example.com/ bar'),
`foo <a href="http://www.example.com/">http://www.example.com/</a> bar`
);
t.is(
filters.urlize('foo www.example.com bar'),
`foo <a href="https://www.example.com">www.example.com</a> bar`
);
t.is(
filters.urlize('foo example.com bar'),
`foo <a href="https://example.com">example.com</a> bar`
);
t.is(
filters.urlize('foo@example.com'),
`<a href="mailto:foo@example.com">foo@example.com</a>`
);
});

test('sort should just defer to localeSort', t => {
Expand Down Expand Up @@ -360,7 +395,7 @@ test('upper should uppercase a string', t => {

test('reverse should reverse a string or array', t => {
t.is(filters.reverse('foobar'), 'raboof');
t.deepEqual(filters.reverse([ 1, 2, 3, 4, 5 ]), [ 5, 4, 3, 2, 1 ]);
t.deepEqual(filters.reverse([1, 2, 3, 4, 5]), [5, 4, 3, 2, 1]);
});

test('string should stringify a value', t => {
Expand All @@ -369,7 +404,7 @@ test('string should stringify a value', t => {
});

test('unique should filter out repeated items from an array', t => {
t.deepEqual(filters.unique([ 1, 2, 3, 3, 2, 1 ]), [ 1, 2, 3 ]);
t.deepEqual(filters.unique([1, 2, 3, 3, 2, 1]), [1, 2, 3]);
});

test('trim newlines should trim a string of line breaks', t => {
Expand All @@ -379,10 +414,25 @@ test('trim newlines should trim a string of line breaks', t => {

test('replace should replace a substring of a string', t => {
t.is(filters.replace('thisisastringisa', 'isa'), 'thisstring');
t.is(filters.replace('thisisastringisa', 'isa', 'wasa'), 'thiswasastringwasa');
t.is(
filters.replace('thisisastringisa', 'isa', 'wasa'),
'thiswasastringwasa'
);
});

test('replaceFirst should replace the first instance of a substring of a string', t => {
t.is(filters.replaceFirst('thisisastringisa', 'isa'), 'thisstringisa');
t.is(filters.replaceFirst('thisisastringisa', 'isa', 'wasa'), 'thiswasastringisa');
t.is(
filters.replaceFirst('thisisastringisa', 'isa', 'wasa'),
'thiswasastringisa'
);
});

test('split should split a string into an array', t => {
t.deepEqual(filters.split('foobar'), ['f', 'o', 'o', 'b', 'a', 'r']);
});

test('urlEncode / UrlDecode should do what they say on the tin', t => {
t.is(filters.urlDecode('foo%20bar'), 'foo bar');
t.is(filters.urlEncode('foo bar'), 'foo%20bar');
});

0 comments on commit c811311

Please sign in to comment.