From 43b6a995d40418ed63510d4914185041cc8e303d Mon Sep 17 00:00:00 2001 From: David Chambers Date: Sun, 29 Apr 2018 23:30:25 +0200 Subject: [PATCH] =?UTF-8?q?remove=20=E2=80=98toString=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function is unnecessary now that sanctuary-show exists. --- bench/README.md | 4 +- bench/old-vs-new.js | 3 - index.js | 131 -------------------------------------------- package.json | 3 +- test/Identity.js | 5 +- test/List.js | 5 +- test/Maybe.js | 5 +- test/Sum.js | 5 +- test/Tuple.js | 5 +- test/eq.js | 4 +- test/index.js | 59 -------------------- 11 files changed, 22 insertions(+), 207 deletions(-) diff --git a/bench/README.md b/bench/README.md index e1d8392..8fb9378 100644 --- a/bench/README.md +++ b/bench/README.md @@ -5,10 +5,10 @@ * Install node modules required for the benchmark you wish to run * Use `npm run bench -- --help` for options -For example, let's say you like to know how the performance of `toString` +For example, let's say you like to know how the performance of `equals` compares to what it was at version 2.x.x: ```console $ (cd bench && npm install sanctuary-type-classes@2.x.x) -$ npm run bench -- --benchmark old-vs-new --match methods.toString.* +$ npm run bench -- --benchmark old-vs-new --match methods.equals.* ``` diff --git a/bench/old-vs-new.js b/bench/old-vs-new.js index ef8335b..86d7795 100644 --- a/bench/old-vs-new.js +++ b/bench/old-vs-new.js @@ -49,9 +49,6 @@ module.exports = benchmark(oldZ, newZ, {leftHeader: 'old', rightHeader: 'new'}, 'methods.map.Identity': function(Z) { Z.map(inc, Identity(1)); }, 'methods.sort.Array': function(Z) { Z.sort(shuffledArray); }, 'methods.sort.List': function(Z) { Z.sort(shuffledList); }, - 'methods.toString.Identity': function(Z) { Z.toString(Identity(0)); }, - 'methods.toString.Object': function(Z) { Z.toString({x: 0, y: 0}); }, - 'methods.toString.String': function(Z) { Z.toString('hello'); }, 'test.Comonad.Identity': function(Z) { Z.Comonad.test(Identity(0)); }, 'test.Contravariant.Function': function(Z) { Z.Contravariant.test(Math.abs); } })); diff --git a/index.js b/index.js index f1e3240..8960bbf 100644 --- a/index.js +++ b/index.js @@ -606,11 +606,6 @@ //. ``` var Contravariant = $('Contravariant', [], {contramap: Value}); - // Null$prototype$toString :: Null ~> () -> String - function Null$prototype$toString() { - return 'null'; - } - // Null$prototype$equals :: Null ~> Null -> Boolean function Null$prototype$equals(other) { return true; @@ -621,11 +616,6 @@ return true; } - // Undefined$prototype$toString :: Undefined ~> () -> String - function Undefined$prototype$toString() { - return 'undefined'; - } - // Undefined$prototype$equals :: Undefined ~> Undefined -> Boolean function Undefined$prototype$equals(other) { return true; @@ -636,13 +626,6 @@ return true; } - // Boolean$prototype$toString :: Boolean ~> () -> String - function Boolean$prototype$toString() { - return typeof this === 'object' ? - 'new Boolean(' + toString(this.valueOf()) + ')' : - this.toString(); - } - // Boolean$prototype$equals :: Boolean ~> Boolean -> Boolean function Boolean$prototype$equals(other) { return typeof this === 'object' ? @@ -657,13 +640,6 @@ this === false || other === true; } - // Number$prototype$toString :: Number ~> () -> String - function Number$prototype$toString() { - return typeof this === 'object' ? - 'new Number(' + toString(this.valueOf()) + ')' : - 1 / this === -Infinity ? '-0' : this.toString(10); - } - // Number$prototype$equals :: Number ~> Number -> Boolean function Number$prototype$equals(other) { return typeof this === 'object' ? @@ -678,12 +654,6 @@ isNaN(this) || this <= other; } - // Date$prototype$toString :: Date ~> () -> String - function Date$prototype$toString() { - var x = isNaN(this.valueOf()) ? NaN : this.toISOString(); - return 'new Date(' + toString(x) + ')'; - } - // Date$prototype$equals :: Date ~> Date -> Boolean function Date$prototype$equals(other) { return equals(this.valueOf(), other.valueOf()); @@ -709,13 +679,6 @@ return ''; } - // String$prototype$toString :: String ~> () -> String - function String$prototype$toString() { - return typeof this === 'object' ? - 'new String(' + toString(this.valueOf()) + ')' : - JSON.stringify(this); - } - // String$prototype$equals :: String ~> String -> Boolean function String$prototype$equals(other) { return typeof this === 'object' ? @@ -775,19 +738,6 @@ return []; } - // Array$prototype$toString :: Array a ~> () -> String - function Array$prototype$toString() { - var reprs = this.map(toString); - var keys = Object.keys(this).sort(); - for (var idx = 0; idx < keys.length; idx += 1) { - var k = keys[idx]; - if (!/^\d+$/.test(k)) { - reprs.push(toString(k) + ': ' + toString(this[k])); - } - } - return '[' + reprs.join(', ') + ']'; - } - // Array$prototype$equals :: Array a ~> Array a -> Boolean function Array$prototype$equals(other) { if (other.length !== this.length) return false; @@ -875,12 +825,6 @@ return this.map(function(_, idx, xs) { return f(xs.slice(idx)); }); } - // Arguments$prototype$toString :: Arguments ~> String - function Arguments$prototype$toString() { - var args = Array.prototype.map.call(this, toString).join(', '); - return '(function () { return arguments; }(' + args + '))'; - } - // Arguments$prototype$equals :: Arguments ~> Arguments -> Boolean function Arguments$prototype$equals(other) { return Array$prototype$equals.call(this, other); @@ -891,11 +835,6 @@ return Array$prototype$lte.call(this, other); } - // Error$prototype$toString :: Error ~> () -> String - function Error$prototype$toString() { - return 'new ' + this.name + '(' + toString(this.message) + ')'; - } - // Error$prototype$equals :: Error ~> Error -> Boolean function Error$prototype$equals(other) { return equals(this.name, other.name) && @@ -912,17 +851,6 @@ return {}; } - // Object$prototype$toString :: StrMap a ~> () -> String - function Object$prototype$toString() { - var reprs = []; - var keys = Object.keys(this).sort(); - for (var idx = 0; idx < keys.length; idx += 1) { - var k = keys[idx]; - reprs.push(toString(k) + ': ' + toString(this[k])); - } - return '{' + reprs.join(', ') + '}'; - } - // Object$prototype$equals :: StrMap a ~> StrMap a -> Boolean function Object$prototype$equals(other) { var self = this; @@ -1076,35 +1004,30 @@ var implementations = { Null: { 'prototype': { - 'toString': Null$prototype$toString, 'fantasy-land/equals': Null$prototype$equals, 'fantasy-land/lte': Null$prototype$lte } }, Undefined: { 'prototype': { - 'toString': Undefined$prototype$toString, 'fantasy-land/equals': Undefined$prototype$equals, 'fantasy-land/lte': Undefined$prototype$lte } }, Boolean: { 'prototype': { - 'toString': Boolean$prototype$toString, 'fantasy-land/equals': Boolean$prototype$equals, 'fantasy-land/lte': Boolean$prototype$lte } }, Number: { 'prototype': { - 'toString': Number$prototype$toString, 'fantasy-land/equals': Number$prototype$equals, 'fantasy-land/lte': Number$prototype$lte } }, Date: { 'prototype': { - 'toString': Date$prototype$toString, 'fantasy-land/equals': Date$prototype$equals, 'fantasy-land/lte': Date$prototype$lte } @@ -1117,7 +1040,6 @@ String: { 'fantasy-land/empty': String$empty, 'prototype': { - 'toString': String$prototype$toString, 'fantasy-land/equals': String$prototype$equals, 'fantasy-land/lte': String$prototype$lte, 'fantasy-land/concat': String$prototype$concat @@ -1129,7 +1051,6 @@ 'fantasy-land/chainRec': Array$chainRec, 'fantasy-land/zero': Array$zero, 'prototype': { - 'toString': Array$prototype$toString, 'fantasy-land/equals': Array$prototype$equals, 'fantasy-land/lte': Array$prototype$lte, 'fantasy-land/concat': Array$prototype$concat, @@ -1145,14 +1066,12 @@ }, Arguments: { 'prototype': { - 'toString': Arguments$prototype$toString, 'fantasy-land/equals': Arguments$prototype$equals, 'fantasy-land/lte': Arguments$prototype$lte } }, Error: { 'prototype': { - 'toString': Error$prototype$toString, 'fantasy-land/equals': Error$prototype$equals } }, @@ -1160,7 +1079,6 @@ 'fantasy-land/empty': Object$empty, 'fantasy-land/zero': Object$zero, 'prototype': { - 'toString': Object$prototype$toString, 'fantasy-land/equals': Object$prototype$equals, 'fantasy-land/lte': Object$prototype$lte, 'fantasy-land/concat': Object$prototype$concat, @@ -1190,54 +1108,6 @@ }; /* eslint-enable key-spacing */ - //# toString :: a -> String - //. - //. Returns a useful string representation of its argument. - //. - //. Dispatches to the argument's `toString` method if appropriate. - //. - //. Where practical, `equals(eval(toString(x)), x) = true`. - //. - //. `toString` implementations are provided for the following built-in types: - //. Null, Undefined, Boolean, Number, Date, String, Array, Arguments, Error, - //. and Object. - //. - //. ```javascript - //. > toString(-0) - //. '-0' - //. - //. > toString(['foo', 'bar', 'baz']) - //. '["foo", "bar", "baz"]' - //. - //. > toString({x: 1, y: 2, z: 3}) - //. '{"x": 1, "y": 2, "z": 3}' - //. - //. > toString(Cons(1, Cons(2, Cons(3, Nil)))) - //. 'Cons(1, Cons(2, Cons(3, Nil)))' - //. ``` - var toString = (function() { - // $seen :: Array Any - var $seen = []; - - function call(method, x) { - $seen.push(x); - try { return method.call(x); } finally { $seen.pop(); } - } - - return function toString(x) { - if ($seen.indexOf(x) >= 0) return ''; - - var xType = type(x); - if (xType === 'Object') { - var result; - try { result = call(x.toString, x); } catch (err) {} - if (result != null && result !== '[object Object]') return result; - } - - return call(implPath([xType, 'prototype', 'toString']) || x.toString, x); - }; - }()); - //# equals :: (a, b) -> Boolean //. //. Returns `true` if its arguments are of the same type and equal according @@ -2345,7 +2215,6 @@ Extend: Extend, Comonad: Comonad, Contravariant: Contravariant, - toString: toString, equals: equals, lt: lt, lte: lte, diff --git a/package.json b/package.json index 272cf81..ba75eec 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "jsverify": "0.8.x", "list": "2.0.11", "sanctuary-benchmark": "1.0.x", - "sanctuary-scripts": "1.5.x" + "sanctuary-scripts": "1.5.x", + "sanctuary-show": "1.0.x" }, "files": [ "/LICENSE", diff --git a/test/Identity.js b/test/Identity.js index 51185cd..72f8fef 100644 --- a/test/Identity.js +++ b/test/Identity.js @@ -1,6 +1,7 @@ 'use strict'; var FL = require('fantasy-land'); +var show = require('sanctuary-show'); var Z = require('..'); @@ -56,8 +57,8 @@ Identity.prototype[FL.extract] = function() { }; Identity.prototype.inspect = -Identity.prototype.toString = function() { - return 'Identity(' + Z.toString(this.value) + ')'; +Identity.prototype['@@show'] = function() { + return 'Identity (' + show(this.value) + ')'; }; module.exports = Identity; diff --git a/test/List.js b/test/List.js index 6ea1cbd..f34e425 100644 --- a/test/List.js +++ b/test/List.js @@ -1,6 +1,7 @@ 'use strict'; var FL = require('fantasy-land'); +var show = require('sanctuary-show'); var Z = require('..'); @@ -93,10 +94,10 @@ List.prototype[FL.traverse] = function(typeRep, f) { }; List.prototype.inspect = -List.prototype.toString = function() { +List.prototype['@@show'] = function() { return this.isNil ? 'Nil' : - 'Cons(' + Z.toString(this.head) + ', ' + Z.toString(this.tail) + ')'; + 'Cons (' + show(this.head) + ', ' + show(this.tail) + ')'; }; module.exports = List; diff --git a/test/Maybe.js b/test/Maybe.js index 6686993..b2f3f68 100644 --- a/test/Maybe.js +++ b/test/Maybe.js @@ -1,6 +1,7 @@ 'use strict'; var FL = require('fantasy-land'); +var show = require('sanctuary-show'); var Z = require('..'); @@ -79,8 +80,8 @@ Maybe.prototype[FL.reduce] = function(f, x) { }; Maybe.prototype.inspect = -Maybe.prototype.toString = function() { - return this.isJust ? 'Just(' + Z.toString(this.value) + ')' : 'Nothing'; +Maybe.prototype['@@show'] = function() { + return this.isJust ? 'Just (' + show(this.value) + ')' : 'Nothing'; }; module.exports = Maybe; diff --git a/test/Sum.js b/test/Sum.js index 8c34d8d..d4206fb 100644 --- a/test/Sum.js +++ b/test/Sum.js @@ -1,6 +1,7 @@ 'use strict'; var FL = require('fantasy-land'); +var show = require('sanctuary-show'); var Z = require('..'); @@ -28,8 +29,8 @@ Sum.prototype[FL.invert] = function() { }; Sum.prototype.inspect = -Sum.prototype.toString = function() { - return 'Sum(' + Z.toString(this.value) + ')'; +Sum.prototype['@@show'] = function() { + return 'Sum (' + show(this.value) + ')'; }; module.exports = Sum; diff --git a/test/Tuple.js b/test/Tuple.js index d1786ab..06a21c3 100644 --- a/test/Tuple.js +++ b/test/Tuple.js @@ -1,6 +1,7 @@ 'use strict'; var FL = require('fantasy-land'); +var show = require('sanctuary-show'); var Z = require('..'); @@ -31,8 +32,8 @@ Tuple.prototype[FL.reduce] = function(f, x) { }; Tuple.prototype.inspect = -Tuple.prototype.toString = function() { - return 'Tuple(' + Z.toString(this._1) + ', ' + Z.toString(this._2) + ')'; +Tuple.prototype['@@show'] = function() { + return 'Tuple (' + show(this._1) + ', ' + show(this._2) + ')'; }; module.exports = Tuple; diff --git a/test/eq.js b/test/eq.js index a1866cc..33129cc 100644 --- a/test/eq.js +++ b/test/eq.js @@ -2,12 +2,14 @@ var assert = require('assert'); +var show = require('sanctuary-show'); + var Z = require('..'); // eq :: (Any, Any) -> Undefined ! module.exports = function eq(actual, expected) { assert.strictEqual(arguments.length, eq.length); - assert.strictEqual(Z.toString(actual), Z.toString(expected)); + assert.strictEqual(show(actual), show(expected)); assert.strictEqual(Z.equals(actual, expected), true); }; diff --git a/test/index.js b/test/index.js index daeb594..4c07678 100644 --- a/test/index.js +++ b/test/index.js @@ -560,65 +560,6 @@ test('Contravariant', function() { eq(Z.Contravariant.test(Math.abs), true); }); -test('toString', function() { - eq(Z.toString.length, 1); - eq(Z.toString.name, 'toString'); - - eq(Z.toString(null), 'null'); - eq(Z.toString(undefined), 'undefined'); - eq(Z.toString(false), 'false'); - eq(Z.toString(true), 'true'); - eq(Z.toString(new Boolean(false)), 'new Boolean(false)'); - eq(Z.toString(new Boolean(true)), 'new Boolean(true)'); - eq(Z.toString(0), '0'); - eq(Z.toString(-0), '-0'); - eq(Z.toString(NaN), 'NaN'); - eq(Z.toString(3.14), '3.14'); - eq(Z.toString(Infinity), 'Infinity'); - eq(Z.toString(-Infinity), '-Infinity'); - eq(Z.toString(new Number(0)), 'new Number(0)'); - eq(Z.toString(new Number(-0)), 'new Number(-0)'); - eq(Z.toString(new Number(NaN)), 'new Number(NaN)'); - eq(Z.toString(new Number(3.14)), 'new Number(3.14)'); - eq(Z.toString(new Number(Infinity)), 'new Number(Infinity)'); - eq(Z.toString(new Number(-Infinity)), 'new Number(-Infinity)'); - eq(Z.toString(new Date(0)), 'new Date("1970-01-01T00:00:00.000Z")'); - eq(Z.toString(new Date(42)), 'new Date("1970-01-01T00:00:00.042Z")'); - eq(Z.toString(new Date(NaN)), 'new Date(NaN)'); - eq(Z.toString(new Date('2001-02-03')), 'new Date("2001-02-03T00:00:00.000Z")'); - eq(Z.toString(/def/g), '/def/g'); - eq(Z.toString(''), '""'); - eq(Z.toString('abc'), '"abc"'); - eq(Z.toString('foo "bar" baz'), '"foo \\"bar\\" baz"'); - eq(Z.toString(new String('')), 'new String("")'); - eq(Z.toString(new String('abc')), 'new String("abc")'); - eq(Z.toString(new String('foo "bar" baz')), 'new String("foo \\"bar\\" baz")'); - eq(Z.toString([]), '[]'); - eq(Z.toString(['foo']), '["foo"]'); - eq(Z.toString(['foo', 'bar']), '["foo", "bar"]'); - eq(Z.toString(/x/.exec('xyz')), '["x", "index": 0, "input": "xyz"]'); - eq(Z.toString(function() { var xs = []; xs.z = true; xs.a = true; return xs; }()), '["a": true, "z": true]'); - eq(Z.toString(ones), '[1, ]'); - eq(Z.toString(ones_), '[1, [1, ]]'); - eq(Z.toString(args()), '(function () { return arguments; }())'); - eq(Z.toString(args('foo')), '(function () { return arguments; }("foo"))'); - eq(Z.toString(args('foo', 'bar')), '(function () { return arguments; }("foo", "bar"))'); - eq(Z.toString(new Error('a')), 'new Error("a")'); - eq(Z.toString(new TypeError('b')), 'new TypeError("b")'); - eq(Z.toString({}), '{}'); - eq(Z.toString({x: 1}), '{"x": 1}'); - eq(Z.toString({x: 1, y: 2}), '{"x": 1, "y": 2}'); - eq(Z.toString({y: 1, x: 2}), '{"x": 2, "y": 1}'); - eq(Z.toString(node1), '{"id": 1, "rels": [{"type": "child", "value": {"id": 2, "rels": [{"type": "parent", "value": }]}}]}'); - eq(Z.toString(node2), '{"id": 2, "rels": [{"type": "parent", "value": {"id": 1, "rels": [{"type": "child", "value": }]}}]}'); - eq(Z.toString(Object.create(null)), '{}'); - function Foo() {} - Foo.prototype = {toString: function() { return 'foo'; }}; - eq(Z.toString(Foo.prototype), 'foo'); - eq(Z.toString(new Foo()), 'foo'); - eq(Z.toString(Math.sqrt), 'function sqrt() { [native code] }'); -}); - test('equals', function() { eq(Z.equals.length, 2); eq(Z.equals.name, 'equals');