Skip to content

Commit 3a0ac8f

Browse files
committed
Fixed ./rule endpoint to import from rule/index file
Compilation
1 parent 40555a6 commit 3a0ac8f

29 files changed

+200
-122
lines changed

dist/api.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.getLifeFileFormat = exports.readLifeFile = exports.readPatternCoordinatesFromFile = void 0;
4-
const life106_1 = require("formats/file/life106");
5-
const plaintext_1 = require("formats/file/plaintext");
6-
const rle_1 = require("formats/file/rle");
7-
const life105_1 = require("formats/file/life105");
8-
function readPatternCoordinatesFromFile(data, format) {
3+
exports.writeLifeFile = exports.getLifeFileFormat = exports.readLifeFile = exports.readLifeFileLiveCoordinates = exports.getLifeRuleFormat = exports.isValidLifeRule = exports.makeLifeRule = exports.readLifeRule = exports.CONWAY_LIFE_RULE_DATA = void 0;
4+
const life106_1 = require("./formats/file/life106");
5+
const plaintext_1 = require("./formats/file/plaintext");
6+
const rle_1 = require("./formats/file/rle");
7+
const life105_1 = require("./formats/file/life105");
8+
const util_1 = require("./core/util");
9+
var ruleData_1 = require("./formats/rule/ruleData");
10+
Object.defineProperty(exports, "CONWAY_LIFE_RULE_DATA", { enumerable: true, get: function () { return ruleData_1.CONWAY_LIFE_RULE_DATA; } });
11+
var rule_1 = require("./formats/rule");
12+
Object.defineProperty(exports, "readLifeRule", { enumerable: true, get: function () { return rule_1.readLifeRule; } });
13+
Object.defineProperty(exports, "makeLifeRule", { enumerable: true, get: function () { return rule_1.makeLifeRule; } });
14+
Object.defineProperty(exports, "isValidLifeRule", { enumerable: true, get: function () { return rule_1.isValidLifeRule; } });
15+
Object.defineProperty(exports, "getLifeRuleFormat", { enumerable: true, get: function () { return rule_1.getLifeRuleFormat; } });
16+
function readLifeFileLiveCoordinates(data, format) {
917
switch (format) {
10-
case "plaintext": return (0, plaintext_1.readPlainTextString)(data).cellCoordinates;
18+
case "plaintext": return (0, plaintext_1.readPlainTextString)(data).liveCoordinates;
1119
case "life 1.06": return (0, life106_1.readLife106String)(data);
12-
case "life 1.05": return (0, life105_1.readLife105String)(data).cellCoordinates;
13-
case "rle": return (0, rle_1.readRLEString)(data).coordinates;
20+
case "life 1.05": return (0, life105_1.readLife105String)(data).liveCoordinates;
21+
case "rle": return (0, rle_1.readRLEString)(data).liveCoordinates;
1422
case "": {
1523
const format = getLifeFileFormat(data);
1624
if (format !== "N/A") {
17-
return readPatternCoordinatesFromFile(data, format);
25+
return readLifeFileLiveCoordinates(data, format);
1826
}
1927
throw new Error("");
2028
}
2129
}
2230
throw new Error("");
2331
}
24-
exports.readPatternCoordinatesFromFile = readPatternCoordinatesFromFile;
32+
exports.readLifeFileLiveCoordinates = readLifeFileLiveCoordinates;
2533
function readLifeFile(data, format = "") {
34+
if (format === null || format === undefined) {
35+
throw new Error("Cannot parse null or undefined life file");
36+
}
2637
const foundFormat = format === "" ? getLifeFileFormat(data) : format;
2738
switch (foundFormat) {
2839
case "plaintext": return (0, plaintext_1.readPlainTextString)(data);
2940
case "life 1.06": return (0, life106_1.readLife106String)(data);
3041
case "rle": return (0, rle_1.readRLEString)(data);
3142
case "life 1.05": return (0, life105_1.readLife105String)(data);
32-
case "N/A": throw new Error(`[libcaread] Could not read life file: matching life file format could not be found`);
43+
case "N/A": throw new Error(`[llcacodecjs] Could not read life file: matching life file format could not be found`);
44+
default: throw new Error(`[llcacodecjs] Could not read life file: Invalid format of ${format} was inputted `);
3345
}
3446
}
3547
exports.readLifeFile = readLifeFile;
@@ -49,3 +61,35 @@ function getLifeFileFormat(data) {
4961
return "N/A";
5062
}
5163
exports.getLifeFileFormat = getLifeFileFormat;
64+
function isPlainTextMatrixWriteData(data) {
65+
return typeof (data) === "object" && data !== null &&
66+
"name" in data && "description" in data && "matrix" in data &&
67+
typeof (data.name) === "string" && (typeof (data.description) === "string" || (0, util_1.isStringArray)(data.description)) &&
68+
(0, util_1.isCellMatrix)(data.matrix);
69+
}
70+
function isPlainTextCoordinateWriteData(data) {
71+
return typeof (data) === "object" && data !== null &&
72+
"name" in data && "description" in data && "liveCoordinates" in data &&
73+
typeof (data.name) === "string" && (typeof (data.description) === "string" || (0, util_1.isStringArray)(data.description)) &&
74+
(0, util_1.isCellCoordinateArray)(data.liveCoordinates);
75+
}
76+
function writeLifeFile(format, data) {
77+
switch (format) {
78+
case "life 1.06": {
79+
if ((0, util_1.isCellCoordinateArray)(data)) {
80+
return (0, life106_1.writeLife106String)(data);
81+
}
82+
throw new Error(`[llcacodecjs] `);
83+
}
84+
case "plaintext": {
85+
if (isPlainTextMatrixWriteData(data)) {
86+
return (0, plaintext_1.writePlainTextMatrix)(data.matrix, data.name, data.description);
87+
}
88+
else if (isPlainTextCoordinateWriteData(data)) {
89+
return (0, plaintext_1.writePlainTextFromCoordinates)(data.liveCoordinates, data.name, data.description);
90+
}
91+
throw new Error(`[llcacodecjs] `);
92+
}
93+
}
94+
}
95+
exports.writeLifeFile = writeLifeFile;

dist/core/util.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.isStrictEqualBooleanArray = exports.isStrictEqualStringArray = exports.isStrictEqualNumberArray = exports.isStrictEqualArray = exports.byteArrayAsASCII = exports.pushASCIIBytes = exports.reverseBits = exports.getErrorMessage = exports.isError = exports.numberPairArrayToMatrix = exports.getCellBoundingBox = exports.isNumberPairArray = exports.isIntegerString = exports.trimTrailing = exports.isNumericString = exports.isDigit = exports.throws = exports.isRectangularMatrix = void 0;
3+
exports.isStrictEqualBooleanArray = exports.isStrictEqualStringArray = exports.isStrictEqualNumberArray = exports.isStrictEqualArray = exports.byteArrayAsASCII = exports.pushASCIIBytes = exports.reverseBits = exports.getErrorMessage = exports.isError = exports.numberPairArrayToMatrix = exports.getCellBoundingBox = exports.isStringArray = exports.isCellCoordinateArray = exports.isCellMatrix = exports.isNumberPairArray = exports.isIntegerString = exports.trimTrailing = exports.isNumericString = exports.isDigit = exports.throws = exports.isRectangularMatrix = void 0;
44
/**
55
* Test if a matrix is rectangular or not
66
*
@@ -73,10 +73,22 @@ function isIntegerString(num) {
7373
return true;
7474
}
7575
exports.isIntegerString = isIntegerString;
76-
function isNumberPairArray(num) {
77-
return num.every(row => row.length === 2);
76+
function isNumberPairArray(arr) {
77+
return Array.isArray(arr) && arr.every(row => Array.isArray(row) && row.length === 2);
7878
}
7979
exports.isNumberPairArray = isNumberPairArray;
80+
function isCellMatrix(arr) {
81+
return Array.isArray(arr) && arr.every(row => Array.isArray(row) && row.every(cell => cell === 0 || cell === 1));
82+
}
83+
exports.isCellMatrix = isCellMatrix;
84+
function isCellCoordinateArray(arr) {
85+
return Array.isArray(arr) && arr.every(row => Array.isArray(row) && row.length === 2 && row.every(component => Number.isInteger(component)));
86+
}
87+
exports.isCellCoordinateArray = isCellCoordinateArray;
88+
function isStringArray(arr) {
89+
return Array.isArray(arr) && arr.every(val => typeof (val) === "string");
90+
}
91+
exports.isStringArray = isStringArray;
8092
/**
8193
*
8294
* @param positions

dist/formats/file/life105.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Life 1.05 File Format Spec: https://conwaylife.com/wiki/Life_1.05
33
Object.defineProperty(exports, "__esModule", { value: true });
44
exports.hello = exports.readLife105String = exports.isLife105String = void 0;
5-
const strRead_1 = require("core/strRead");
6-
const rule_1 = require("formats/rule");
5+
const strRead_1 = require("../../core/strRead");
6+
const rule_1 = require("../rule");
77
const LIFE_105_HEADER = "#Life 1.05";
88
const MAX_DESCRIPTION_LINE_COUNT = 22;
99
const LIFE_105_MAX_LINE_LENGTH = 80;
@@ -18,7 +18,7 @@ function readLife105CellBlock(data) {
1818
width: 0,
1919
height: 0,
2020
pattern: [],
21-
cellCoordinates: []
21+
liveCoordinates: []
2222
};
2323
const [pointLine, afterPointLine] = (0, strRead_1.readLine)(data);
2424
const [, afterPointDeclaration] = (0, strRead_1.readChars)(pointLine, "#P");
@@ -32,7 +32,7 @@ function readLife105CellBlock(data) {
3232
for (let i = 0; i < cellBlock.width; i++) {
3333
if (currentLine[i] === LIFE_105_ALIVE_CHAR) {
3434
row[i] = 1;
35-
cellBlock.cellCoordinates.push([x + i, y - cellBlock.height]);
35+
cellBlock.liveCoordinates.push([x + i, y - cellBlock.height]);
3636
}
3737
}
3838
cellBlock.pattern.push(row);
@@ -63,7 +63,7 @@ function readLife105String(file) {
6363
file = file.replace("\r", "");
6464
const life105FileData = {
6565
cellBlocks: [],
66-
cellCoordinates: [],
66+
liveCoordinates: [],
6767
descriptions: [],
6868
ruleString: rule_1.CONWAY_RULE_STRING_SB,
6969
rule: (0, rule_1.CONWAY_LIFE_RULE_DATA)(),
@@ -109,7 +109,7 @@ function readLife105String(file) {
109109
console.log("Read cell block: ", cellBlockReadingData[0]);
110110
console.log("Remaining: ", cellBlockReadingData[1]);
111111
life105FileData.cellBlocks.push(cellBlockReadingData[0]);
112-
life105FileData.cellCoordinates.push(...cellBlockReadingData[0].cellCoordinates);
112+
life105FileData.liveCoordinates.push(...cellBlockReadingData[0].liveCoordinates);
113113
remainingCellBlocksString = cellBlockReadingData[1];
114114
}
115115
catch (err) {

dist/formats/file/life106.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.readLife106String = exports.writeLife106String = exports.isLife106String = void 0;
4-
const util_1 = require("core/util");
5-
const util_2 = require("core/util");
6-
const set2D_1 = require("core/set2D");
4+
const util_1 = require("../../core/util");
5+
const set2D_1 = require("../../core/set2D");
76
const LIFE_106_HEADER = "#Life 1.06";
87
const LIFE_106_FILE_EXTENSIONS = [".lif", ".life"];
98
function isLife106String(str) {
@@ -57,7 +56,7 @@ function readLife106String(str) {
5756
if (ended) {
5857
throw new Error(`Invalid Life 1.06 string: \n${str}\n Error at Line ${i}. X and Y Values must be on subsequent lines`);
5958
}
60-
if ((0, util_2.isIntegerString)(nums[0]) && (0, util_2.isIntegerString)(nums[1])) {
59+
if ((0, util_1.isIntegerString)(nums[0]) && (0, util_1.isIntegerString)(nums[1])) {
6160
const [x, y] = [Number.parseInt(nums[0]), Number.parseInt(nums[1])];
6261
if (!set2D.has(x, y)) {
6362
set2D.add(x, y);

dist/formats/file/plaintext.js

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.isPlainTextDiagram = exports.readPlainTextString = exports.isPlainTextString = exports.writePlainTextMatrix = exports.writePlainTextFromCoordinates = exports.readPlainTextDiagramToRowColCoordinates = exports.readPlainTextDiagramToMatrix = exports.readPlainTextDiagramToXYCoordinates = void 0;
4-
const util_1 = require("core/util");
5-
const util_2 = require("core/util");
6-
const strRead_1 = require("core/strRead");
3+
exports.isPlainTextDiagram = exports.readPlainTextDiagramToMatrix = exports.readPlainTextDiagramToXY = exports.readPlainTextString = exports.isPlainTextString = exports.writePlainTextMatrix = exports.writePlainTextFromCoordinates = void 0;
4+
const util_1 = require("../../core/util");
5+
const strRead_1 = require("../../core/strRead");
76
const VALID_DEAD_CELL_CHARACTERS = ["."];
87
const VALID_LIVE_CELL_CHARACTERS = ["O", "*"];
8+
// --------------------------------------------------------------
9+
// --------------------------------------------------------------
10+
// --------------------------------------------------------------
11+
// ------------------------- WRITING ----------------------------
12+
// --------------------------------------------------------------
13+
// --------------------------------------------------------------
14+
// --------------------------------------------------------------
915
function writePlainTextMetadata(byteArray, name, description) {
1016
const newArray = [...byteArray];
1117
(0, util_1.pushASCIIBytes)(newArray, "!Name: " + name + "\n");
1218
if (description.length > 0) {
1319
if (typeof (description) === "string") {
14-
const lines = description.split("\n");
20+
const lines = description.replace("\r", "").split("\n");
1521
lines.forEach(line => (0, util_1.pushASCIIBytes)(newArray, `!${line}\n`));
1622
}
1723
else {
@@ -22,52 +28,13 @@ function writePlainTextMetadata(byteArray, name, description) {
2228
(0, util_1.pushASCIIBytes)(newArray, "!\n");
2329
return newArray;
2430
}
25-
function readPlainTextDiagramToXYCoordinates(str) {
26-
if (isPlainTextDiagram(str)) {
27-
const lines = str.split("\n");
28-
return lines.flatMap((line, row) => line.split("").map((char, col) => VALID_LIVE_CELL_CHARACTERS.some(valid => valid === char) ? [col, -row] : []).filter(point => point.length > 0));
29-
}
30-
throw new Error("error");
31-
}
32-
exports.readPlainTextDiagramToXYCoordinates = readPlainTextDiagramToXYCoordinates;
33-
function readPlainTextDiagramToMatrix(str) {
34-
if (isPlainTextDiagram(str)) {
35-
const lines = (0, util_1.trimTrailing)(str, "\n").split("\n");
36-
const width = Math.max(...lines.map(line => line.length));
37-
return lines.map(line => {
38-
const newLine = new Array(width);
39-
for (let i = 0; i < width; i++) {
40-
if (i >= line.length) {
41-
newLine[i] = 0;
42-
}
43-
else if (VALID_LIVE_CELL_CHARACTERS.some(ch => ch === line[i])) {
44-
newLine[i] = 1;
45-
}
46-
else if (VALID_DEAD_CELL_CHARACTERS.some(ch => ch === line[i])) {
47-
newLine[i] = 0;
48-
}
49-
else if (line[i] !== " ") {
50-
throw new Error();
51-
}
52-
}
53-
return newLine;
54-
});
55-
}
56-
throw new Error("");
57-
}
58-
exports.readPlainTextDiagramToMatrix = readPlainTextDiagramToMatrix;
59-
function readPlainTextDiagramToRowColCoordinates(str) {
60-
const coordinates = readPlainTextDiagramToXYCoordinates(str);
61-
return coordinates.map(coord => ([coord[0], -coord[1]]));
62-
}
63-
exports.readPlainTextDiagramToRowColCoordinates = readPlainTextDiagramToRowColCoordinates;
6431
function writePlainTextFromCoordinates(positions, name, description) {
6532
for (let i = 0; i < positions.length; i++) {
6633
if (!Number.isInteger(positions[i][0]) || !Number.isInteger(positions[i][1])) {
6734
throw new Error(`Attempted to write plain text with Invalid Coordinates: Coordinates must all be integers (Error at coordinate #${i} x: ${positions[i][0]} y: ${positions[i][1]} `);
6835
}
6936
}
70-
return writePlainTextMatrix((0, util_2.numberPairArrayToMatrix)(positions), name, description);
37+
return writePlainTextMatrix((0, util_1.numberPairArrayToMatrix)(positions), name, description);
7138
}
7239
exports.writePlainTextFromCoordinates = writePlainTextFromCoordinates;
7340
function writePlainTextMatrix(data, name, description) {
@@ -88,6 +55,13 @@ function writePlainTextMatrix(data, name, description) {
8855
return (0, util_1.byteArrayAsASCII)(byteArray);
8956
}
9057
exports.writePlainTextMatrix = writePlainTextMatrix;
58+
// --------------------------------------------------------------
59+
// --------------------------------------------------------------
60+
// --------------------------------------------------------------
61+
// ------------------------- READING ----------------------------
62+
// --------------------------------------------------------------
63+
// --------------------------------------------------------------
64+
// --------------------------------------------------------------
9165
function isPlainTextString(str) {
9266
try {
9367
readPlainTextString(str);
@@ -102,15 +76,15 @@ function readPlainTextString(str) {
10276
if (str.length === 0) {
10377
throw new Error("");
10478
}
105-
const lines = str.split("\n").map(line => line.trim());
79+
const lines = str.replace("\r", "").split("\n").map(line => line.trim());
10680
if (lines.length === 0) {
10781
throw new Error("");
10882
}
10983
const contents = {
11084
name: "",
11185
description: [],
11286
matrix: [],
113-
cellCoordinates: []
87+
liveCoordinates: []
11488
};
11589
//reads header line
11690
if ((0, strRead_1.isNextChar)(lines[0], "!")) {
@@ -129,7 +103,7 @@ function readPlainTextString(str) {
129103
name: "",
130104
description: [],
131105
matrix: readPlainTextDiagramToMatrix(trimmedStr),
132-
cellCoordinates: readPlainTextDiagramToXYCoordinates(trimmedStr)
106+
liveCoordinates: readPlainTextDiagramToXY(trimmedStr)
133107
};
134108
}
135109
throw new Error("");
@@ -145,7 +119,7 @@ function readPlainTextString(str) {
145119
}
146120
const diagramLines = lines.slice(currentLine).join("\n");
147121
if (isPlainTextDiagram(diagramLines)) {
148-
contents.cellCoordinates = readPlainTextDiagramToXYCoordinates(diagramLines);
122+
contents.liveCoordinates = readPlainTextDiagramToXY(diagramLines);
149123
contents.matrix = readPlainTextDiagramToMatrix(diagramLines);
150124
}
151125
else {
@@ -154,6 +128,40 @@ function readPlainTextString(str) {
154128
return contents;
155129
}
156130
exports.readPlainTextString = readPlainTextString;
131+
function readPlainTextDiagramToXY(str) {
132+
if (isPlainTextDiagram(str)) {
133+
const lines = str.split("\n");
134+
return lines.flatMap((line, row) => line.split("").map((char, col) => VALID_LIVE_CELL_CHARACTERS.some(valid => valid === char) ? [col, -row] : []).filter(point => point.length > 0));
135+
}
136+
throw new Error("error");
137+
}
138+
exports.readPlainTextDiagramToXY = readPlainTextDiagramToXY;
139+
function readPlainTextDiagramToMatrix(str) {
140+
if (isPlainTextDiagram(str)) {
141+
const lines = (0, util_1.trimTrailing)(str, "\n").split("\n");
142+
const width = Math.max(...lines.map(line => line.length));
143+
return lines.map(line => {
144+
const newLine = new Array(width);
145+
for (let i = 0; i < width; i++) {
146+
if (i >= line.length) {
147+
newLine[i] = 0;
148+
}
149+
else if (VALID_LIVE_CELL_CHARACTERS.some(ch => ch === line[i])) {
150+
newLine[i] = 1;
151+
}
152+
else if (VALID_DEAD_CELL_CHARACTERS.some(ch => ch === line[i])) {
153+
newLine[i] = 0;
154+
}
155+
else if (line[i] !== " ") {
156+
throw new Error();
157+
}
158+
}
159+
return newLine;
160+
});
161+
}
162+
throw new Error("");
163+
}
164+
exports.readPlainTextDiagramToMatrix = readPlainTextDiagramToMatrix;
157165
function isPlainTextDiagram(line) {
158166
return line.split("").every(char => VALID_DEAD_CELL_CHARACTERS.some(ch => ch === char) || VALID_LIVE_CELL_CHARACTERS.some(ch => ch === char) || char === " " || char === "\n");
159167
}

0 commit comments

Comments
 (0)