Imagine we want to use the amazing [`chalk`](https://github.com/chalk/chalk) js library for terminal styling in our purescript code.
We start by installing chalk.
```
npm install chalk
```
`chalk` defines a number of functions and combinators that allow you to write coloured text to your terminal. For instance the function `red` from `chalk` prints a red string. In js you would use it like this:
```js
import chalk from 'chalk';
console.log(chalk.red("Red velvet 🎂"));
```
Let's see how we can ffi this in our purescript code:
```purescript
main :: Effect Unit
main = launchAff_ do
{ red } <-fromDefault"chalk"
log $ red "Red velvet 🎂"
```
Running it will print:
![red velvet in red letters](./assets/red-velvet.png)
You can easily import the `default` export from a module using `fromDefault`. This will type red as `String -> String`.
js typically uses uncurried functions (e.g. `f(a,b,c) `) instead of curried functions (e.g. `f(a)(b)(c)` ) like purescript. Use `curried` to use the uncurried js function as a curried purescript function:
js functions are sometimes designed to be variadic, i.e. to have a variable number of arguments. Use `variadic myModuleOrFunc func` for functions that accept varargs:
```purescript
let
example1 = variadic func arg1
example1 = variadic func arg1 arg2 arg3
example1 = variadic func [arg1, arg2, arg3, arg4]
```
#### Eample
In fact, the colour methods in `chalk` are variadic methods (as you have seen in the previous example) and you can pass an arbitrary number of arguments:
Sometimes js functions don't work in purescript, because they internally use `this` which fails to resolve in a curried contex. Use `scoped myModuleOrFunc func` you to make a function scoped:
```purescript
result <-scopedmyModuleOrFuncfuncarg1arg2
```
#### Example
Simply using the `rgb` function from `chalk` as we did before will fail because it uses `this` internally. To make it work again we will need to set the scope for the function to the module:
js functions very often cause side-effects (e.g. like printing to the console). Use `effectful` you to catch these side-effects and return `Effect` instead:
Clearly, `post` is an effectful function, as it triggers a promise. So in purescript `post` will have the signature `Effect (Promise json)` which we can then be converted to an `Aff` using the `toAffE`.
We import the `post` from `got`. The first thing we need to do is uncurry it, since it receives two arguments, the url and the json record. We then wrap it using `effectful` so that is run in an `Effect`. We can then call the effectful js function `json()` on the resulting `Promise` to get the body, so we can just `bind` (`>>=`) it.
`fuse.js` is a library for fuzzy search. In js you use it by first creating a new `Fuse` object using `new` and passing the data as a list and some options:
```js
const fuse = new Fuse(list, options);
```
Then you can search on this object:
```js
const pattern = "my-search-pattern"
fuse.search(pattern)
```
We first import the default export from `fuse.js` which gives us the class, which we can then pass to `new` to create the `Fuse` object:
Kudos to @paluh for writing the [`purescript-js-object`](https://github.com/paluh/purescript-js-object) library (check it out!), which basically inspired me to write this library. @paluh's library is probably the (type-)safer option, but I thought if I am already too lazy to write ffi, then I really want to be lazy and not write anything at all. So this library follows a different implementation approach to basically not require any ffi code, at the expense of more type-safety.