Skip to content

Instantly share code, notes, and snippets.

@think49
Last active September 24, 2018 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/2322863df508fa5fc1a77cb6e7efc55e to your computer and use it in GitHub Desktop.
Save think49/2322863df508fa5fc1a77cb6e7efc55e to your computer and use it in GitHub Desktop.
coordinate-list.js: Coordinate(座標を格納するクラス), CoordinateList(座標リストを格納するクラス)

coordinate-list.js

概要

  • Coordinate … 座標を格納するクラス
  • CoordinateList … 座標リストを格納するクラス

Coordinate

const c1 = new Coordinate([['x', 1], ['y', 2]]);

console.log(JSON.stringify(c1));	// {"dimensionName":"x y","x":1,"y":2}
console.log(c1.keys());					 // ["x", "y"]
console.log(c1.values());				 // [1, 2]
console.log(c1.entries());			 // [["x", 1],["y", 2]]

c1.set([' y ', 3]);
console.log(c1.entries());			 // [["x", 1],["y", 3]]
c1.set(['z', 4]);
console.log(c1.entries());			 // [["x", 1],["y", 3],["y", 4]]

new Coordinate([['x', 'foo']]);  // TypeError: foo is not a number
new Coordinate([['x y', 1]]);    // Error: Do not include white-spaces in the "key": x y

CoordinateList

const list = new CoordinateList([[['x', 1], ['y', 2]], [['x', 2], ['y', 3]], [['x', 3], ['y', 4]]]);

console.log(JSON.stringify(list)); // {"dimensionName":"x y","item":[{"dimensionName":"x y","x":1,"y":2},{"dimensionName":"x y","x":2,"y":3},{"dimensionName":"x y","x":3,"y":4}]}
console.log(list.entries());	     // [[["x",1],["y",2]],[["x",2],["y",3]],[["x",3],["y",4]]]
console.log(list.getCenter());     // Coordinate {dimensionName: "x y", x: 2, y: 3,...} (中心座標を返す)

new CoordinateList([[['x', 1], ['y', 2]], [['z', 1], ['t', 2]]]); // Error: All coordinate.dimensionName must be identical
/**
* coordinate-list-0.1.0.js
* - Coordinate ... Class for storing coordinates.
* - CoordinateList ... Class that stores coordinate list.
*
* @version 0.1.0
* @author think49
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
* @url https://gist.github.com/think49/2322863df508fa5fc1a77cb6e7efc55e
*/
'use strcit';
class Coordinate {
constructor (dimensionList) {
Object.defineProperty(this, '__map__', {value: new Map});
this.dimensionName = '';
if (dimensionList) {
this.set(...dimensionList);
}
}
keys () {
return [...this.__map__.keys()];
}
values () {
return [...this.__map__.values()];
}
entries () {
return [...this.__map__.entries()];
}
get size () {
return this.__map__.size;
}
set size (value) {
}
set (...dimensionList) {
const map = this.__map__;
for (let [key, value] of dimensionList) {
const validKey = String(key).trim(), validValue = +value;
if (/\s/.test(validKey)) {
throw new Error('Do not include white-spaces in the "key": ' + key);
}
if (isNaN(validValue)) {
throw new TypeError(value + ' is not a number');
}
map.set(validKey, validValue);
this[validKey] = validValue;
}
this.dimensionName = this.keys().join(' ');
}
}
class CoordinateList {
constructor (coordinateList) {
this.dimensionName = '';
this.item = [];
if (coordinateList) {
this.push(...coordinateList);
}
}
entries () {
const entries = [];
for (let coordinate of this.item) {
entries.push(coordinate.entries());
}
return entries;
}
push (...coordinateList) {
const item = this.item;
let dimensionName = this.dimensionName;
for (let dimensionList of coordinateList) {
const coordinate = new Coordinate(dimensionList);
if (!dimensionName) {
dimensionName = this.dimensionName = coordinate.dimensionName;
} else if (dimensionName !== coordinate.dimensionName) {
throw new Error('All coordinate.dimensionName must be identical');
}
item.push(coordinate);
}
return item.length;
}
getCenter () {
const item = this.item, length = item.length;
if (!length) {
return new Coordinate;
}
const center = new Coordinate(item[0].entries());
for (let i = 1; i < length; ++i) {
const target = item[i];
for (let [key, value] of center.entries()) {
center.set([key, value + target[key]]);
}
}
for (let [key, value] of center.entries()) {
center.set([key, value / length]);
}
return center;
}
}
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<title>test</title>
<style>
</style>
</head>
<body>
<script src="coordinate-list-0.1.0.js"></script>
<script>
'use strict';
{
const c1 = new Coordinate([['x', 1], ['y', 2]]);
console.assert(c1.dimensionName === 'x y');
console.assert(JSON.stringify(c1.keys()) === '["x","y"]');
console.assert(JSON.stringify(c1.values()) === '[1,2]');
console.assert(JSON.stringify(c1.entries()) === '[["x",1],["y",2]]');
console.assert(c1.size === 2);
console.assert(c1.x === 1);
console.assert(c1.y === 2);
const c2 = new Coordinate;
c2.set([' x ', 1], [' y ', 2]);
console.assert(c2.dimensionName === 'x y');
console.assert(JSON.stringify(c2.keys()) === '["x","y"]');
console.assert(JSON.stringify(c2.values()) === '[1,2]');
console.assert(JSON.stringify(c2.entries()) === '[["x",1],["y",2]]');
console.assert(c2.size === 2);
console.assert(c2.x === 1);
console.assert(c2.y === 2);
console.assert(JSON.stringify(c1) === JSON.stringify(c2));
c2.set(['z', 'foo']); // TypeError: foo is not a number
}
</script>
<script>
'use strict';
new Coordinate([['x y', 1]]); // Error: Do not include white-spaces in the "key": x y
</script>
<script>
'use strict';
{
const c1 = new CoordinateList([[['x', 1], ['y', 2]], [['x', 2], ['y', 3]], [['x', 3], ['y', 4]]]);
console.assert(c1.dimensionName === 'x y');
console.assert(JSON.stringify(c1.getCenter().entries()) === '[["x",2],["y",3]]');
console.assert(JSON.stringify(c1.entries()) === '[[["x",1],["y",2]],[["x",2],["y",3]],[["x",3],["y",4]]]');
const c2 = new CoordinateList;
c2.push([['x', 1], ['y', 2]], [['x', 2], ['y', 3]], [['x', 3], ['y', 4]]);
console.assert(JSON.stringify(c2.getCenter().entries()) === '[["x",2],["y",3]]');
console.assert(JSON.stringify(c1.entries()) === JSON.stringify(c2.entries()));
new CoordinateList([[['x', 1], ['y', 2]], [['z', 1], ['t', 2]]]); // Error: All coordinate.dimensionName must be identical
}
</script>
<script>
'use strict';
</script>
<script>
'use strict';
</script>
<script>
'use strict';
</script>
<script>
'use strict';
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment