Repository: sanctuary-js/sanctuary
Pull request #460 (2017-11-23 01:17:53)

Add value constructor for string maps
Followup from [here](https://github.com/sanctuary-js/sanctuary/issues/434#issuecomment-346505904). Name is up for debate, I think `embed` might be nicer.

Something is wrong with my setup it looks like, because tests explode when I run `npm test`. Punting to the CI.

```
node_modules/.bin/istanbul cover node_modules/.bin/_mocha
No coverage information was collected, exit without writing coverage information
/mnt/d/depot/git/sanctuary/node_modules/.bin/_mocha:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Object.Module._extensions.(anonymous function) [as .js] (/mnt/d/depot/git/sanctuary/node_modules/istanbul/lib/hook.js:109:37)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at runFn (/mnt/d/depot/git/sanctuary/node_modules/istanbul/lib/command/common/run-with-cover.js:122:16)
Makefile:58: recipe for target 'test' failed
make: *** [test] Error 1
npm ERR! Test failed.  See above for more details.
```

davidchambers: Thanks for the pull request, @masaeedu!

> Name is up for debate, I think `embed` might be nicer.

It doesn't seem quite right to me:

> __embed__
>
> 1.  fix (an object) firmly and deeply in a surrounding mass: _he had an operation to remove a nail **embedded in** his chest_.

The suggestion of depth is misleading in the context of this function.

I don't claim that `singleton` is the ideal name; I simply searched Hayoo for [`k -> v -> Map k v`][1]. I'm open to other ideas. :)

> Something is wrong with my setup it looks like, because tests explode when I run `npm test`.

Let's discuss this on Gitter.


[1]: http://hayoo.fh-wedel.de/?query=k+-%3E+v+-%3E+Map+k+v

masaeedu: @davidchambers Ok, new and improved, hopefully totally nit-free commits pushed.
davidchambers: The changes look great, @masaeedu!

I'd like to leave this pull request open for a few days to give others the opportunity to comment on the additions generally, and on the function name specifically.

gabejohnson: ```js
S.equals( S.singleton("foo", 1), S.insert("foo", 1, {}) )
```
once #435 is merged. Correct?

I know they're not semantically equivalent and there's a minor perf hit with using `insert` for this purpose, but I thought I'd mention it.

Patch:
diff --git a/index.js b/index.js
index 4c313d6..ee0aa78 100644
--- a/index.js
+++ b/index.js
@@ -3741,6 +3741,22 @@
   //. [type identifier][] is `'Object'` and the values of its enumerable own
   //. properties are all members of type `a`.
 
+  //# singleton :: String -> a -> StrMap a
+  //.
+  //. Takes a string and a value of any type, and returns a string map with
+  //. a single entry (mapping the key to the value).
+  //.
+  //. ```javascript
+  //. > S.singleton('foo', 42)
+  //. {foo: 42}
+  //. ```
+  function singleton(key, val) {
+    var strMap = {};
+    strMap[key] = val;
+    return strMap;
+  }
+  S.singleton = def('singleton', {}, [$.String, a, $.StrMap(a)], singleton);
+
   //# keys :: StrMap a -> Array String
   //.
   //. Returns the keys of the given string map, in arbitrary order.


Test patch:
diff --git a/test/singleton.js b/test/singleton.js
new file mode 100644
index 0000000..7e765da
--- /dev/null
+++ b/test/singleton.js
@@ -0,0 +1,12 @@
+'use strict';
+
+var S = require('..');
+
+var eq = require('./internal/eq');
+
+
+test('singleton', function() {
+
+  eq(S.singleton('foo', 42), {foo: 42});
+
+});