Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
19 changes: 19 additions & 0 deletions .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: Bug report
about: Report an issue
title: ""
labels: bug
assignees: ""
---

**Describe the bug**
A clear and concise description of the bug.

**To Reproduce**
A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue.

**Expected behavior**
A clear and concise description of what you expected to happen.

**Additional context**
Add any other context about the problem here.
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/change-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Change request
about: Propose an improvement to this library
title: ""
labels: ""
assignees: ""
---

**Is your change request related to a problem? Please describe.**
A clear and concise description of the problem.

Examples:

- It's frustrating to have to [...]
- I was looking for a function to [...]

**Describe the solution you'd like**
A clear and concise description of what a good solution to you looks like, including any solutions you've already considered.

**Additional context**
Add any other context about the change request here.
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: PureScript Discourse
url: https://discourse.purescript.org/
about: Ask and answer questions on the PureScript discussion forum.
- name: PureScript Discord
url: https://purescript.org/chat
about: Ask and answer questions on the PureScript chat.
11 changes: 11 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
**Description of the change**
Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR.

---

**Checklist:**

- [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username
- [ ] Linked any existing issues or proposals that this pull request should close
- [ ] Updated or added relevant documentation in the README and/or documentation directory
- [ ] Added a test for the contribution (if applicable)
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
branches: [master, main]
pull_request:
branches: [master, main]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up a PureScript toolchain
uses: purescript-contrib/setup-purescript@main
with:
purescript: "latest"
purs-tidy: "0.11.0" # "latest"
spago: "unstable"

- name: Cache PureScript dependencies
uses: actions/cache@v4
with:
key: ${{ runner.os }}-spago-${{ hashFiles('**/spago.lock') }}
path: |
.spago
output

- name: Build source
run: spago build --censor-stats --strict --ensure-ranges --pedantic-packages

- name: Run tests
run: spago test --offline --censor-stats --strict --pedantic-packages

- name: Verify formatting
run: purs-tidy check src test examples/src
23 changes: 14 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
.DS_Store
.psci
.psci_modules
.pulp-cache/
npm-debug.log
node_modules/
bower_components/
tmp/
output/
.*
!.gitignore
!.github
!.editorconfig
!.tidyrc.json
!.eslintrc.json

output
generated-docs
bower_components

node_modules
package-lock.json
*.lock
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

Breaking changes:

New features:

Bugfixes:

Other improvements:

## [v4.0.0](https://github.com/ethul/purescript-undefinable/releases/tag/v4.0.0) - 2019-01-26

- Initial release
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contributing to Undefinable

Thanks for your interest in contributing to `undefinable`! We welcome new contributions regardless of your level of experience or familiarity with PureScript.

Every library in the Contributors organization shares a simple handbook that helps new contributors get started. With that in mind, please [read the short contributing guide on ethul/governance](https://github.com/ethul/governance/blob/main/contributing.md) before contributing to this library.
106 changes: 102 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,107 @@
# purescript-undefinable
# Undefinable

A library for handling `undefined` values.
[![CI](https://github.com/ethul/purescript-undefinable/workflows/CI/badge.svg?branch=main)](https://github.com/ethul/purescript-undefinable/actions?query=workflow%3ACI+branch%3Amain)
[![Release](https://img.shields.io/github/release/ethul/purescript-undefinable.svg)](https://github.com/ethul/purescript-undefinable/releases)
[![Pursuit](https://pursuit.purescript.org/packages/purescript-undefinable/badge)](https://pursuit.purescript.org/packages/purescript-undefinable)

A library for dealing with undefined values in foreign libraries.

`undefinable` is intended to be used with PureScript's [Foreign Function Interface (FFI)](https://github.com/purescript/documentation/blob/master/guides/FFI.md). If you are looking for general-purpose optional values, use [`Maybe`](https://github.com/purescript/purescript-maybe) instead.

## Installation

```bash
bower install --save purescript-undefinable
Install `undefinable` with [Spago](https://github.com/purescript/spago):

```sh
spago install undefinable
```

## Quick start

> The following example lives in [`examples`](./examples) and can be built and run using `spago -x examples.dhall run`.

Here we have a little JavaScript function that we can use to determine whether a number is unlucky. We give it some number and it will either return `undefined` if the number is considered "unlucky" or just return the number otherwise:

```js
export function unluckyImpl(n) {
// Unlucky number 13!
if (n === 13) {
return undefined; // but not null
}

return n;
}
```

If we want to use this function from PureScript we'll need to go through the FFI:

```purescript
module QuickStart
( unlucky -- We only want to expose our "safe" `unlucky` function outside of
-- this module and keep the backing implementation (`unluckyImpl`)
-- hidden.
) where

import Prelude
import Data.Function.Uncurried (Fn1, runFn1)
import Data.Maybe (Maybe)
import Data.Undefinable (Undefinable, toMaybe)

-- Here we declare a binding to a foreign JavaScript function that we'll call
-- out to using the FFI.
--
-- This function takes an `Int` and then returns either an integer or a `undefined`
-- based on the given value. We use `Undefinable Int` to indicate that we could
-- get a `undefined` back from this function.
foreign import unluckyImpl :: Fn1 Int (Undefinable Int)

-- We don't want to have to use `Undefinable` in our PureScript code, so we can use
-- `toMaybe` to convert our `Undefinable Int` into a `Maybe Int` which will then be
-- part of the API visible outside of this module.
unlucky :: Int -> Maybe Int
unlucky n = toMaybe $ runFn1 unluckyImpl n
```

You can run the following to load this example up in the REPL:

```
cd examples && spago repl
```

Once the REPL is loaded, go ahead and add the following imports:

```purescript
import Prelude
import Data.Maybe
import QuickStart
```

You can now test out our `unlucky` function in the REPL:

```purescript
unlucky 7 == Just 7
unlucky 13 == Nothing
```

## Documentation

`undefinable` documentation is stored in a few places:

1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-undefinable).
2. Written documentation is kept in the [docs directory](./docs).
3. Usage examples can be found in [the test suite](./test).

If you get stuck, there are several ways to get help:

- [Open an issue](https://github.com/ethul/purescript-undefinable/issues) if you have encountered a bug or problem.
- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://purescript.org/chat) chat.

## Contributing

You can contribute to `undefinable` in several ways:

1. If you encounter a problem or have a question, please [open an issue](https://github.com/ethul/purescript-undefinable/issues). We'll do our best to work with you to resolve or answer it.

2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions.

3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed.
24 changes: 0 additions & 24 deletions bower.json

This file was deleted.

3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Undefinable Documentation

This directory contains documentation for `undefinable`. If you are interested in contributing new documentation, please read the [contributor guidelines](../CONTRIBUTING.md) and [What Nobody Tells You About Documentation](https://documentation.divio.com) for help getting started.
17 changes: 17 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import js from "@eslint/js";
import globals from "globals";
import { defineConfig } from "eslint/config";
import { includeIgnoreFile } from "@eslint/compat";
import { fileURLToPath } from "node:url";

const gitignorePath = fileURLToPath(new URL(".gitignore", import.meta.url));

export default defineConfig([
includeIgnoreFile(gitignorePath),
{
files: ["**/*.{js,mjs,cjs}"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: globals.browser }
},
]);
15 changes: 15 additions & 0 deletions examples/spago.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package:
name: undefinable-example
dependencies:
- effect
- prelude
- maybe
- functions
- undefinable
- console
workspace:
packageSet:
registry: 64.4.0
extraPackages:
undefinable:
path: ..
11 changes: 11 additions & 0 deletions examples/src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Main where

import Prelude
import Effect (Effect)
import Effect.Console (logShow)
import QuickStart (unlucky)

main :: Effect Unit
main = do
logShow $ unlucky 7
logShow $ unlucky 13
8 changes: 8 additions & 0 deletions examples/src/QuickStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function unluckyImpl(n) {
// Unlucky number 13!
if (n === 13) {
return undefined;
}

return n;
}
24 changes: 24 additions & 0 deletions examples/src/QuickStart.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module QuickStart
( unlucky -- We only want to expose our "safe" `unlucky` function outside of
-- this module and keep the backing implementation (`unluckyImpl`)
-- hidden.
) where

import Prelude
import Data.Function.Uncurried (Fn1, runFn1)
import Data.Maybe (Maybe)
import Data.Undefinable (Undefinable, toMaybe)

-- Here we declare a binding to a foreign JavaScript function that we'll call
-- out to using the FFI.
--
-- This function takes an `Int` and then returns either an integer or a `undefined`
-- based on the given value. We use `Undefinable Int` to indicate that we could
-- get a `undefined` back from this function.
foreign import unluckyImpl :: Fn1 Int (Undefinable Int)

-- We don't want to have to use `Undefinable` in our PureScript code, so we can use
-- `toMaybe` to convert our `Undefinable Int` into a `Maybe Int` which will then be
-- part of the API visible outside of this module.
unlucky :: Int -> Maybe Int
unlucky n = toMaybe $ runFn1 unluckyImpl n
Loading