Control Flow Primitives for displaying given number or a number range of elements.
npm install @solid-primitives/range



Control Flow Primitives for displaying a number range or given number of elements.
- repeat - Primitive for mapping a number of elements. Underlying helper for the control flow.
- - Control Flow Component for displaying a number of elements.
- mapRange - Primitive for mapping a number range of given start, end, and step values. Underlying helper for the control flow.
- - Control Flow Component for displaying a number range of elements.
- indexRange - Primitive for mapping a number range while keeping previous elements of the same index. Underlying helper for the control flow.
- - Control Flow Component for displaying a number range of elements, where elements receive a number value as signal.
``bash`
npm install @solid-primitives/rangeor
yarn add @solid-primitives/range
Reactively maps a number range of specified length with a callback function - underlying helper for the
`ts`
const [length, setLength] = createSignal(10)
const mapped = repeat(length, index => {
const [value, setValue] = createSignal(index);
createEffect(() => {...})
return value
})
#### Definition
`ts`
function repeat
times: Accessor
mapFn: (i: number) => T,
options?: {
fallback?: Accessor
},
): Accessor
Control Flow component for displaying a specified number of elements.
The times prop is reactive – changing it will only create new elements for added numbers.
`tsx
// with a render prop:
{n => {n}}
// with fallback:
#### Definition
`ts
function Repeat(props: {
times: number;
fallback?: T;
children: ((index: number) => T) | T;
}): Accessor;
`mapRangeReactively maps a number range of specified
stop, to and step, with a callback function - underlying helper for the control flow.All
stop, to and step arguments are accessors, and changing them will cause the mapped array to be recalculated, mapping new items for numbers added to the range.step will become negative _(the range will be descending)_ if to is smaller than start. Range stops at to, it is not included in the range.`ts
const [to, setTo] = createSignal(5)
const mapped = mapRange(() => 0, to, () => 0.5, number => {
const [value, setValue] = createSignal(number);
createEffect(() => {...})
return value
})
mapped() // => [0, 0.5, 1, 1.5, 2...]
setTo(3) // changes the output array, mapping only added numbers
`#### Definition
`ts
function mapRange(
start: Accessor,
to: Accessor,
step: Accessor,
mapFn: (n: number) => T,
options?: {
fallback?: Accessor;
},
): Accessor;
`Creates a list of elements by mapping a number range of specified
start, to, and step.All
stop, to and step props are reactive, and changing them will cause the elements array to be recalculated, creating new elements for numbers added to the range.-
start defaults to 0.-
to defaults to 1. Range stops at to, it is not included in the range.-
step will become negative _(the range will be descending)_ if to is smaller than start.`tsx
// with a render prop:
{n => {n}}
// with fallback:
no items...
}>
Array spread shortcut:
`tsx
const [start, setStart] = createSignal(0);
const [to, setTo] = createSignal(10);
const [step, setStep] = createSignal(2);
`#### Definition
RangeProps is an interface of stop, to and step props, OR 0, 1 and 2 indexes of a spread array.`ts
function Range(
props: RangeProps & {
fallback?: T;
children: ((number: number) => T) | T;
},
): Accessor;
`indexRangePrimitive for mapping a number range of specified
stop, to and step, while keeping previous elements of the same index. Underlying helper for the control flow.All
stop, to and step arguments are accessors, and changing them will cause the mapped array to be recalculated, mapping new items appended at the end of the range.step will become negative _(the range will be descending)_ if to is smaller than start. Range stops at to, it is not included in the range.`ts
const [to, setTo] = createSignal(5);
const mapped = indexRange(
() => 0,
to,
() => 0.5,
number => {
const [value, setValue] = createSignal(number());
createEffect(() => handleNewNumber(number()));
return value;
},
);
mapped(); // => [0, 0.5, 1, 1.5, 2...]
setTo(3); // changes the output array, mapping only added indexes
`#### Definition
`ts
function indexRange(
start: Accessor,
to: Accessor,
step: Accessor,
mapFn: (n: Accessor) => T,
options?: {
fallback?: Accessor;
},
): Accessor;
`Control Flow Component for displaying a number range of elements, where elements receive a number value as signal, by mapping a number range of specified
start, to, and step.All
stop, to and step props are reactive, and changing them will cause the elements array to be recalculated, creating new elements for numbers added to the range.-
start defaults to 0.-
to defaults to 1. Range stops at to, it is not included in the range.-
step will become negative _(the range will be descending)_ if to is smaller than start.`tsx
// with a render prop:
{n => {n()}}
// with fallback:
no items...
}>
Array spread shortcut:
`tsx
const [start, setStart] = createSignal(0);
const [to, setTo] = createSignal(10);
const [step, setStep] = createSignal(2);
`#### Definition
RangeProps is an interface of stop, to and step props, OR 0, 1 and 2 indexes of a spread array.`ts
function IndexRange(
props: RangeProps & {
fallback?: T;
children: ((number: Accessor) => T) | T;
},
): Accessor;
`Demo
Codesandbox: https://codesandbox.io/s/solid-primitives-range-demo-y3sc5c?file=/index.tsx
Possible improvements
###### (PRs Welcome)
- [ ] Currently the
mapRange is handling decremanting ranges by swapping start and to with each other, and then cloning and reversing the mapped array. Doing this during the range mapping could possibly be more performant.
- [ ] For Ranges, because of how numbers are calculated, fractions might sometimes loose precision. E.g. a range from 1.64 to 2 by 0.2 step would generate numbers: [1.64, 1.8399999999999999] instead of [1.64, 1.84].
- [ ] Both mapRange and indexRange` are missing index arguments in the mapping function.See CHANGELOG.md