Spaces:
Sleeping
Sleeping
File size: 5,574 Bytes
391a73c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | <p align="center">
<img src="https://user-images.githubusercontent.com/5600341/27505816-c8bc37aa-587f-11e7-9a86-08a2d081a8b9.png" height="280px">
</p>
# @react-pdf/fns
> Lightweight utility functions for react-pdf
A collection of functional programming utilities used internally by react-pdf. Zero dependencies, tree-shakeable, and fully typed.
## Installation
```bash
yarn add @react-pdf/fns
```
## Table of Contents
- [adjust](#adjust)
- [asyncCompose](#asynccompose)
- [capitalize](#capitalize)
- [castArray](#castarray)
- [compose](#compose)
- [dropLast](#droplast)
- [evolve](#evolve)
- [get](#get)
- [isNil](#isnil)
- [last](#last)
- [mapValues](#mapvalues)
- [matchPercent](#matchpercent)
- [omit](#omit)
- [parseFloat](#parsefloat)
- [pick](#pick)
- [repeat](#repeat)
- [reverse](#reverse)
- [upperFirst](#upperfirst)
- [without](#without)
## Functions
### adjust
Applies a function to the value at the given index of an array.
```js
import { adjust } from '@react-pdf/fns';
adjust(1, (x) => x * 2, [1, 2, 3]); // => [1, 4, 3]
adjust(-1, (x) => x + 10, [1, 2, 3]); // => [1, 2, 13]
```
---
### asyncCompose
Performs right-to-left function composition with async functions support. `asyncCompose(f, g, h)(x)` is equivalent to `await f(await g(await h(x)))`.
```js
import { asyncCompose } from '@react-pdf/fns';
const addAsync = async (x) => x + 1;
const double = (x) => x * 2;
const fn = asyncCompose(double, addAsync);
await fn(5); // => 12
```
---
### capitalize
Capitalizes the first letter of each word in a string.
```js
import { capitalize } from '@react-pdf/fns';
capitalize('hello world'); // => 'Hello World'
capitalize('foo bar baz'); // => 'Foo Bar Baz'
```
---
### castArray
Wraps a value in an array if it isn't one already.
```js
import { castArray } from '@react-pdf/fns';
castArray('foo'); // => ['foo']
castArray(['foo']); // => ['foo']
castArray(123); // => [123]
```
---
### compose
Performs right-to-left function composition. `compose(f, g, h)(x)` is equivalent to `f(g(h(x)))`.
```js
import { compose } from '@react-pdf/fns';
const add1 = (x) => x + 1;
const double = (x) => x * 2;
const fn = compose(double, add1);
fn(5); // => 12
```
---
### dropLast
Drops the last element from an array or string.
```js
import { dropLast } from '@react-pdf/fns';
dropLast([1, 2, 3]); // => [1, 2]
dropLast('hello'); // => 'hell'
```
---
### evolve
Applies transformations to an object's values based on a transformation map.
```js
import { evolve } from '@react-pdf/fns';
evolve(
{ count: (n) => n + 1, name: (s) => s.toUpperCase() },
{ name: 'item', count: 5 },
);
// => { name: 'ITEM', count: 6 }
```
---
### get
Retrieves a value at a given path from an object with a default fallback.
```js
import { get } from '@react-pdf/fns';
get({ a: { b: 1 } }, ['a', 'b'], 0); // => 1
get({ a: { b: 1 } }, ['a', 'c'], 0); // => 0
get({ a: { b: 1 } }, 'a', {}); // => { b: 1 }
```
---
### isNil
Checks if a value is `null` or `undefined`.
```js
import { isNil } from '@react-pdf/fns';
isNil(null); // => true
isNil(undefined); // => true
isNil(0); // => false
isNil(''); // => false
```
---
### last
Returns the last element of an array or last character of a string.
```js
import { last } from '@react-pdf/fns';
last([1, 2, 3]); // => 3
last('abc'); // => 'c'
last([]); // => undefined
```
---
### mapValues
Maps over the values of an object, applying a function to each value.
```js
import { mapValues } from '@react-pdf/fns';
mapValues({ a: 1, b: 2 }, (v) => v * 2); // => { a: 2, b: 4 }
mapValues({ x: 'foo', y: 'bar' }, (v, k) => `${k}:${v}`);
// => { x: 'x:foo', y: 'y:bar' }
```
---
### matchPercent
Parses a percentage string and returns both the numeric value and decimal percent.
```js
import { matchPercent } from '@react-pdf/fns';
matchPercent('50%'); // => { value: 50, percent: 0.5 }
matchPercent('-25%'); // => { value: -25, percent: -0.25 }
matchPercent('abc'); // => null
```
---
### omit
Creates a new object excluding specified keys.
```js
import { omit } from '@react-pdf/fns';
omit('b', { a: 1, b: 2, c: 3 }); // => { a: 1, c: 3 }
omit(['a', 'c'], { a: 1, b: 2, c: 3 }); // => { b: 2 }
```
---
### parseFloat
Parses a string to a float. Non-string values pass through unchanged.
```js
import { parseFloat } from '@react-pdf/fns';
parseFloat('3.14'); // => 3.14
parseFloat('10px'); // => 10
parseFloat(42); // => 42
parseFloat(null); // => null
```
---
### pick
Creates a new object with only the specified keys.
```js
import { pick } from '@react-pdf/fns';
pick(['a', 'c'], { a: 1, b: 2, c: 3 }); // => { a: 1, c: 3 }
pick(['x'], { a: 1, b: 2 }); // => {}
```
---
### repeat
Creates an array with an element repeated a specified number of times.
```js
import { repeat } from '@react-pdf/fns';
repeat('a', 3); // => ['a', 'a', 'a']
repeat(0, 4); // => [0, 0, 0, 0]
```
---
### reverse
Returns a new array with elements in reverse order (does not mutate original).
```js
import { reverse } from '@react-pdf/fns';
reverse([1, 2, 3]); // => [3, 2, 1]
reverse(['a', 'b', 'c']); // => ['c', 'b', 'a']
```
---
### upperFirst
Converts the first character of a string to uppercase.
```js
import { upperFirst } from '@react-pdf/fns';
upperFirst('hello'); // => 'Hello'
upperFirst('hELLO'); // => 'HELLO'
```
---
### without
Returns a new array excluding the specified values.
```js
import { without } from '@react-pdf/fns';
without([2, 4], [1, 2, 3, 4, 5]); // => [1, 3, 5]
without(['b'], ['a', 'b', 'c']); // => ['a', 'c']
```
## License
MIT
|