Skip to content

Commit

Permalink
Version 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Nov 3, 2019
1 parent a87cb8e commit f0dc566
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Sanctuary
Copyright (c) 2019 Sanctuary

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
68 changes: 22 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ sanctuary-type-identifiers comprises:

For a type to be compatible with the algorithm:

- every member of the type MUST have a `constructor` property
pointing to an object known as the _type representative_;

- the type representative MUST have a `@@type` property
- every member of the type MUST have a `@@type` property
(the _type identifier_); and

- the type identifier MUST be a string primitive and SHOULD have
Expand All @@ -45,83 +42,62 @@ _namespace_ will be `null` and _version_ will be `0`.

If the _version_ is not given, it is assumed to be `0`.

For example:

```javascript
// Identity :: a -> Identity a
function Identity(x) {
if (!(this instanceof Identity)) return new Identity(x);
this.value = x;
}

Identity['@@type'] = 'my-package/Identity';
```

Note that by using a constructor function the `constructor` property is set
implicitly for each value created. Constructor functions are convenient for
this reason, but are not required. This definition is also valid:

```javascript
// IdentityTypeRep :: TypeRep Identity
var IdentityTypeRep = {
'@@type': 'my-package/Identity'
};

// Identity :: a -> Identity a
function Identity(x) {
return {constructor: IdentityTypeRep, value: x};
}
```

### Usage

```javascript
const type = require('sanctuary-type-identifiers');
const type = require ('sanctuary-type-identifiers');
```

```javascript
> function Identity(x) {
. if (!(this instanceof Identity)) return new Identity(x);
. this.value = x;
> const Identity$prototype = {
. '@@type': 'my-package/Identity@1',
. '@@show': function() {
. return 'Identity (' + show (this.value) + ')';
. }
. }
. Identity['@@type'] = 'my-package/Identity@1';

> type.parse(type(Identity(0)))
> const Identity = value =>
. Object.assign (Object.create (Identity$prototype), {value})

> type (Identity (0))
'my-package/Identity@1'

> type.parse (type (Identity (0)))
{namespace: 'my-package', name: 'Identity', version: 1}
```

### API

<h4 name="type"><code><a href="https://github.com/sanctuary-js/sanctuary-type-identifiers/blob/v2.0.1/index.js#L138">type :: Any -> String</a></code></h4>
#### <a name="type" href="https://github.com/sanctuary-js/sanctuary-type-identifiers/blob/v3.0.0/index.js#L115">`type :: Any -> String`</a>

Takes any value and returns a string which identifies its type. If the
value conforms to the [specification][4], the custom type identifier is
returned.

```javascript
> type(null)
> type (null)
'Null'

> type(true)
> type (true)
'Boolean'

> type(Identity(0))
> type (Identity (0))
'my-package/Identity@1'
```

<h4 name="type.parse"><code><a href="https://github.com/sanctuary-js/sanctuary-type-identifiers/blob/v2.0.1/index.js#L163">type.parse :: String -> { namespace :: Nullable String, name :: String, version :: Number }</a></code></h4>
#### <a name="type.parse" href="https://github.com/sanctuary-js/sanctuary-type-identifiers/blob/v3.0.0/index.js#L141">`type.parse :: String -> { namespace :: Nullable String, name :: String, version :: Number }`</a>

Takes any string and parses it according to the [specification][4],
returning an object with `namespace`, `name`, and `version` fields.

```javascript
> type.parse('my-package/List@2')
> type.parse ('my-package/List@2')
{namespace: 'my-package', name: 'List', version: 2}

> type.parse('nonsense!')
> type.parse ('nonsense!')
{namespace: null, name: 'nonsense!', version: 0}

> type.parse(Identity['@@type'])
> type.parse (type (Identity (0)))
{namespace: 'my-package', name: 'Identity', version: 1}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanctuary-type-identifiers",
"version": "2.0.1",
"version": "3.0.0",
"description": "Specification for type identifiers",
"license": "MIT",
"repository": {
Expand Down

0 comments on commit f0dc566

Please sign in to comment.