|
| 1 | +# denofun |
| 2 | +Small utility library for [Deno](https://deno.land) containing functions, monads and other fun stuff. |
| 3 | + |
| 4 | +<image src="https://media.giphy.com/media/XOXdQszYm4I3m/giphy.gif" width="480" height="360"> |
| 5 | + |
| 6 | +## Documentation |
| 7 | +Code samples assume [import map](https://deno.land/manual.html#importmaps) is configured like this (URL is still WIP, `denofun` like Deno is still under heavy development). |
| 8 | + |
| 9 | +``` |
| 10 | +{ |
| 11 | + "imports": { |
| 12 | + "std/": "https://deno.land/std/", |
| 13 | + "denofun/": "https://raw.githubusercontent.com/galkowskit/denofun/master/" |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +### compose |
| 19 | + |
| 20 | +```typescript |
| 21 | +import compose from "denofun/lib/compose.ts"; |
| 22 | + |
| 23 | +function greet(name: string) { |
| 24 | + return `hello, ${name}!`; |
| 25 | +} |
| 26 | + |
| 27 | +function makeLoud(x) { |
| 28 | + return x.toUpperCase(); |
| 29 | +} |
| 30 | + |
| 31 | +const greetLoudly = compose(makeLoud, greet); |
| 32 | + |
| 33 | +greetLoudly('world'); // => HELLO, WORLD! |
| 34 | +``` |
| 35 | + |
| 36 | +### curry |
| 37 | + |
| 38 | +```typescript |
| 39 | +import curry from "denofun/lib/curry.ts"; |
| 40 | + |
| 41 | +const greet = (name: string, age: number) => `hello, I'm Tomasz and I'm 26 years old`; |
| 42 | +greet("Tomasz", 26); // => hello, I'm Tomasz and I'm 26 years old |
| 43 | + |
| 44 | +const curriedGreet = curry(greet); |
| 45 | +curriedGreet("Tomasz")(26) // => hello, I'm Tomasz and I'm 26 years old |
| 46 | + |
| 47 | +// === |
| 48 | + |
| 49 | +const cars = [ |
| 50 | + { make: 'Alfa Romeo', model: 'Giulia' }, |
| 51 | + { make: 'Alfa Romeo', model: 'Stelvio' }, |
| 52 | + { make: 'Ford', model: 'Mustang '}, |
| 53 | + { make: 'Ford', model: 'Focus '}, |
| 54 | + { make: 'Toyota', model: 'Mirai' }, |
| 55 | + { make: 'Toyota', model: 'Yaris' }, |
| 56 | + { make: 'Toyota', model: 'Supra' }, |
| 57 | +]; |
| 58 | + |
| 59 | +const filterCarsByMake = curry((make: string, car) => car.make === make); |
| 60 | +const filterAlfas = filterCarsByMake('Alfa Romeo'); |
| 61 | +cars.filter(filterAlfas); // => [ { make: "Alfa Romeo", model: "Giulia" }, { make: "Alfa Romeo", model: "Stelvio" } ] |
| 62 | +``` |
| 63 | + |
| 64 | +### filter |
| 65 | + |
| 66 | +```typescript |
| 67 | +import filter from "denofun/lib/filter.ts"; |
| 68 | + |
| 69 | +const cars = [ |
| 70 | + { make: 'Alfa Romeo', model: 'Giulia' }, |
| 71 | + { make: 'Alfa Romeo', model: 'Stelvio' }, |
| 72 | + { make: 'Ford', model: 'Mustang '}, |
| 73 | + { make: 'Ford', model: 'Focus '}, |
| 74 | + { make: 'Toyota', model: 'Mirai' }, |
| 75 | + { make: 'Toyota', model: 'Yaris' }, |
| 76 | + { make: 'Toyota', model: 'Supra' }, |
| 77 | +]; |
| 78 | + |
| 79 | +filter(car => car.make === 'Ford', cars); // => [ { make: "Ford", model: "Mustang " }, { make: "Ford", model: "Focus " } ] |
| 80 | +``` |
| 81 | + |
| 82 | +### head |
| 83 | + |
| 84 | +```typescript |
| 85 | +import head from "denofun/lib/head.ts"; |
| 86 | + |
| 87 | +const numbers = [1, 2, 3, 4, 5]; |
| 88 | +head(numbers); // => 1 |
| 89 | + |
| 90 | +// === |
| 91 | + |
| 92 | +head("hello world!"); // => "h" |
| 93 | +``` |
| 94 | + |
| 95 | +### identity |
| 96 | + |
| 97 | +```typescript |
| 98 | +import identity from "denofun/lib/identity.ts"; |
| 99 | + |
| 100 | +const x = 5; |
| 101 | +identity(x); // => 5 |
| 102 | +``` |
| 103 | + |
| 104 | +### map |
| 105 | + |
| 106 | +```typescript |
| 107 | +import map from "denofun/lib/map.ts"; |
| 108 | + |
| 109 | +const numbers = [1, 2, 3, 4, 5]; |
| 110 | + |
| 111 | +map(n => n * 2, numbers); // => [2, 4, 6, 8, 10] |
| 112 | +``` |
| 113 | + |
| 114 | +### pipe |
| 115 | + |
| 116 | +```typescript |
| 117 | +import pipe from "denofun/lib/pipe.ts"; |
| 118 | + |
| 119 | +function greet(name: string) { |
| 120 | + return `hello, ${name}!`; |
| 121 | +} |
| 122 | + |
| 123 | +function makeLoud(x) { |
| 124 | + return x.toUpperCase(); |
| 125 | +} |
| 126 | + |
| 127 | +const greetLoudly = pipe(greet, makeLoud); |
| 128 | + |
| 129 | +greetLoudly('world'); // => HELLO, WORLD! |
| 130 | +``` |
| 131 | + |
| 132 | +### reduce |
| 133 | + |
| 134 | +```typescript |
| 135 | +import reduce from "denofun/lib/reduce.ts"; |
| 136 | + |
| 137 | +const numbers = [1, 2, 3, 4, 5]; |
| 138 | + |
| 139 | +const add = (a, b) => a + b; |
| 140 | +reduce(add, 0, numbers); // => 15 |
| 141 | +``` |
| 142 | + |
| 143 | +### reverse |
| 144 | + |
| 145 | +```typescript |
| 146 | +import reverse from "denofun/lib/reverse.ts"; |
| 147 | + |
| 148 | +reverse([1, 2, 3, 4, 5]); // => [5, 4, 3, 2, 1] |
| 149 | + |
| 150 | +// === |
| 151 | + |
| 152 | +reverse("hello world!"); // => ["!dlrow olleh"] |
| 153 | +``` |
| 154 | + |
| 155 | +### tail |
| 156 | + |
| 157 | +```typescript |
| 158 | +import tail from "denofun/lib/tail.ts"; |
| 159 | + |
| 160 | +tail([1, 2, 3, 4, 5]); // => [2, 3, 4, 5] |
| 161 | + |
| 162 | +// === |
| 163 | + |
| 164 | +tail("hello world!"); // => "ello world!" |
| 165 | +``` |
| 166 | + |
| 167 | +## Goals |
| 168 | +- provide a good replacement for libraries like Ramda or Lodash with "native" Deno feel |
| 169 | +- provide basic monads (Either, Maybe/Option) |
| 170 | +- good, out-of-the-box typings |
| 171 | + |
| 172 | +## Non-Goals |
| 173 | +- get integrated into `deno_std` as `fun` module |
| 174 | +- provide more monads (Future, Stream) |
0 commit comments