-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,032 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"use strict"; | ||
|
||
var slice = require("@sinonjs/commons").prototypes.string.slice; | ||
var typeOf = require("@sinonjs/commons").typeOf; | ||
|
||
module.exports = function iterableToString(obj) { | ||
var representation = ""; | ||
|
||
function stringify(item) { | ||
return typeof item === "string" ? "'" + item + "'" : String(item); | ||
} | ||
|
||
function mapToString(map) { | ||
/* eslint-disable-next-line local-rules/no-prototype-methods */ | ||
map.forEach(function(value, key) { | ||
representation += | ||
"[" + stringify(key) + "," + stringify(value) + "],"; | ||
}); | ||
|
||
representation = slice(representation, 0, -1); | ||
return representation; | ||
} | ||
|
||
function genericIterableToString(iterable) { | ||
/* eslint-disable-next-line local-rules/no-prototype-methods */ | ||
iterable.forEach(function(value) { | ||
representation += stringify(value) + ","; | ||
}); | ||
|
||
representation = slice(representation, 0, -1); | ||
return representation; | ||
} | ||
|
||
if (typeOf(obj) === "map") { | ||
return mapToString(obj); | ||
} | ||
|
||
return genericIterableToString(obj); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use strict"; | ||
|
||
var assert = require("@sinonjs/referee").assert; | ||
var iterableToString = require("./iterable-to-string"); | ||
|
||
describe("iterableToString", function() { | ||
it("returns an String representation of Array objects", function() { | ||
var arr = [1, "one", true, undefined, null]; | ||
var expected = "1,'one',true,undefined,null"; | ||
|
||
assert.equals(iterableToString(arr), expected); | ||
}); | ||
|
||
if (typeof Map === "function") { | ||
it("returns an String representation of Map objects", function() { | ||
var map = new Map(); | ||
map.set(1, 1); | ||
map.set("one", "one"); | ||
map.set(true, true); | ||
map.set(undefined, undefined); | ||
map.set(null, null); | ||
var expected = | ||
"[1,1]," + | ||
"['one','one']," + | ||
"[true,true]," + | ||
"[undefined,undefined]," + | ||
"[null,null]"; | ||
|
||
assert.equals(iterableToString(map), expected); | ||
}); | ||
} | ||
|
||
if (typeof Set === "function") { | ||
it("returns an String representation of Set objects", function() { | ||
var set = new Set(); | ||
set.add(1); | ||
set.add("one"); | ||
set.add(true); | ||
set.add(undefined); | ||
set.add(null); | ||
|
||
var expected = "1,'one',true,undefined,null"; | ||
|
||
assert.equals(iterableToString(set), expected); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.