Merge pull request #70 from IBM/refactor

refactor(lib): extract fillArray utility
This commit is contained in:
Eric Liu 2019-12-23 09:50:08 -08:00 committed by GitHub
commit 17730914d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 10 deletions

View file

@ -6,11 +6,11 @@
export let style = undefined; export let style = undefined;
import ChevronRight16 from 'carbon-icons-svelte/lib/ChevronRight16'; import ChevronRight16 from 'carbon-icons-svelte/lib/ChevronRight16';
import { cx } from '../../lib'; import { cx, fillArray } from '../../lib';
import SkeletonText from '../SkeletonText'; import SkeletonText from '../SkeletonText';
const _class = cx('--accordion', '--skeleton', className); const _class = cx('--accordion', '--skeleton', className);
const skeletonItems = Array.from({ length: open ? count - 1 : count }); const items = fillArray(open ? count - 1 : count);
</script> </script>
<ul on:click on:mouseover on:mouseenter on:mouseleave {style} class={_class}> <ul on:click on:mouseover on:mouseenter on:mouseleave {style} class={_class}>
@ -27,7 +27,7 @@
</div> </div>
</li> </li>
{/if} {/if}
{#each skeletonItems as item} {#each items as item, i (item)}
<li class={cx('--accordion__item')}> <li class={cx('--accordion__item')}>
<span class={cx('--accordion__heading')}> <span class={cx('--accordion__heading')}>
<ChevronRight16 class={cx('--accordion__arrow')} /> <ChevronRight16 class={cx('--accordion__arrow')} />

View file

@ -8,10 +8,10 @@
export let headers = []; export let headers = [];
export let style = undefined; export let style = undefined;
import { cx } from '../../lib'; import { cx, fillArray } from '../../lib';
const rows = Array.from({ length: rowCount - 1 }, (_, i) => i); const rows = fillArray(rowCount - 1);
const columns = Array.from({ length: columnCount }, (_, i) => i); const columns = fillArray(columnCount);
const _headers = const _headers =
headers[0] === Object(headers[0]) && !Array.isArray(headers[0]) headers[0] === Object(headers[0]) && !Array.isArray(headers[0])
? headers.map(({ header }) => header) ? headers.map(({ header }) => header)

View file

@ -21,11 +21,11 @@
import CaretRight24 from 'carbon-icons-svelte/lib/CaretRight24'; import CaretRight24 from 'carbon-icons-svelte/lib/CaretRight24';
import CaretLeft24 from 'carbon-icons-svelte/lib/CaretLeft24'; import CaretLeft24 from 'carbon-icons-svelte/lib/CaretLeft24';
import Select, { SelectItem } from '../Select'; import Select, { SelectItem } from '../Select';
import { cx } from '../../lib'; import { cx, fillArray } from '../../lib';
const _class = cx('--pagination', className); const _class = cx('--pagination', className);
$: totalPages = Math.max(Math.ceil(totalItems / pageSize), 1); $: totalPages = Math.max(Math.ceil(totalItems / pageSize), 1);
$: selectItems = Array.from({ length: totalPages }, (_, i) => i); $: selectItems = fillArray(totalPages);
$: backButtonDisabled = disabled || page === 1; $: backButtonDisabled = disabled || page === 1;
$: _backButtonClass = cx( $: _backButtonClass = cx(
'--pagination__button', '--pagination__button',

View file

@ -5,7 +5,7 @@
export let rowCount = 5; export let rowCount = 5;
export let style = undefined; export let style = undefined;
import { cx } from '../../lib'; import { cx, fillArray } from '../../lib';
const _class = cx( const _class = cx(
'--skeleton', '--skeleton',
@ -13,7 +13,7 @@
border && '--structured-list--border', border && '--structured-list--border',
className className
); );
const rows = Array.from({ length: rowCount - 1 }, (_, i) => i); const rows = fillArray(rowCount - 1);
</script> </script>
<section on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}> <section on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}>

View file

@ -0,0 +1,7 @@
import { fillArray } from '../fill-array';
test('fillArray', () => {
expect(fillArray(0)).toEqual([]);
expect(fillArray(2)).toEqual([0, 1]);
expect(fillArray(3)).toEqual([0, 1, 2]);
});

5
src/lib/fill-array.js Normal file
View file

@ -0,0 +1,5 @@
function fillArray(length) {
return Array.from({ length }, (_, i) => i);
}
export { fillArray };

View file

@ -1,2 +1,3 @@
export { cx } from './cx'; export { cx } from './cx';
export { css } from './css'; export { css } from './css';
export { fillArray } from './fill-array';