aboutsummaryrefslogtreecommitdiff
path: root/webAO
diff options
context:
space:
mode:
Diffstat (limited to 'webAO')
-rw-r--r--webAO/__tests__/iniParse.test.js47
-rw-r--r--webAO/client.js39
-rw-r--r--webAO/iniParse.js42
3 files changed, 91 insertions, 37 deletions
diff --git a/webAO/__tests__/iniParse.test.js b/webAO/__tests__/iniParse.test.js
new file mode 100644
index 0000000..deb08f3
--- /dev/null
+++ b/webAO/__tests__/iniParse.test.js
@@ -0,0 +1,47 @@
+import iniParse from '../iniParse';
+
+const iniExample = `
+[Options]
+name = Matt
+showname = Matty
+
+[Emotions]
+number = 9
+1 = Normal#-#normal#0#1
+`;
+describe('iniParse', () => {
+ test('should not lowercase value if key is showname', () => {
+ const parsedIni = iniParse(`
+ [test]
+ showname = MATT
+ `);
+ expect(parsedIni.test.showname).toBe('MATT');
+ });
+ test('should lowercase value if key is not showname', () => {
+ const parsedIni = iniParse(`
+ [test]
+ party = TIME
+ `);
+ expect(parsedIni.test.party).toBe('time');
+ });
+ test('should parse sections', () => {
+ const parsedIni = iniParse(iniExample);
+ expect(Object.keys(parsedIni).length).toBe(2);
+ });
+ test('should parse parameters', () => {
+ const parsedIni = iniParse(iniExample);
+ expect(Object.keys(parsedIni.options).length).toBe(2);
+ });
+ test('should remove empty lines', () => {
+ const parsedIni = iniParse(`
+ [test]
+
+
+ 1 = 1
+ 2 = 2
+
+
+ `);
+ expect(Object.keys(parsedIni.test).length).toBe(2);
+ });
+});
diff --git a/webAO/client.js b/webAO/client.js
index 7ff561b..eb7c7a2 100644
--- a/webAO/client.js
+++ b/webAO/client.js
@@ -18,6 +18,7 @@ import vanilla_background_arr from './backgrounds.js';
import vanilla_evidence_arr from './evidence.js';
import chatbox_arr from './styles/chatbox/chatboxes.js';
+import iniParse from './iniParse';
const version = process.env.npm_package_version;
@@ -843,7 +844,7 @@ class Client extends EventEmitter {
// If the ini doesn't exist on the server this will throw an error
try {
const cinidata = await request(`${AO_HOST}characters/${encodeURI(chargs[0].toLowerCase())}/char.ini`);
- cini = INI.parse(cinidata);
+ cini = iniParse(cinidata);
} catch (err) {
cini = {};
img.classList.add('noini');
@@ -2410,42 +2411,6 @@ class Viewport {
}
}
-class INI {
- static parse(data) {
- const regex = {
- section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
- param: /^\s*([\w.\-_]+)\s*=\s*(.*?)\s*$/,
- comment: /^\s*;.*$/,
- };
- const value = {};
- const lines = data.split(/\r\n|\r|\n/);
- let section;
- lines.forEach((line) => {
- if (regex.comment.test(line)) {
-
- } else if (line.length === 0) {
-
- } else if (regex.param.test(line)) {
- const match = line.match(regex.param);
- if (section) {
- if (match[1].toLowerCase() === 'showname') { // don't lowercase the showname
- value[section].showname = match[2];
- } else {
- value[section][match[1].toLowerCase()] = match[2].toLowerCase();
- }
- // } else { // we don't care about attributes without a section
- // value[match[1]] = match[2];
- }
- } else if (regex.section.test(line)) {
- const match = line.match(regex.section);
- value[match[1].toLowerCase()] = {}; // lowercase everything else
- section = match[1].toLowerCase();
- }
- });
- return value;
- }
-}
-
/**
* read a cookie from storage
* got this from w3schools
diff --git a/webAO/iniParse.js b/webAO/iniParse.js
new file mode 100644
index 0000000..fb04e67
--- /dev/null
+++ b/webAO/iniParse.js
@@ -0,0 +1,42 @@
+const regexPatterns = {
+ section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
+ param: /^\s*([\w.\-_]+)\s*=\s*(.*?)\s*$/,
+ comment: /^\s*;.*$/,
+};
+
+const valueHandler = (matchKey, matchValue) => (matchKey === 'showname' ? matchValue : matchValue.toLowerCase());
+
+const lineFilter = (value) => {
+ const isEmpty = value.length === 0;
+ const isComment = regexPatterns.comment.test(value);
+ if (isComment || isEmpty) {
+ return false;
+ }
+ return true;
+};
+
+const iniParse = (data) => {
+ const parsedIni = {};
+ const lines = data.split(/\r\n|\r|\n/);
+ const filteredLines = lines.filter(lineFilter);
+
+ let currentSection;
+ filteredLines.forEach((line) => {
+ const isParameter = regexPatterns.param.test(line);
+ const isSection = regexPatterns.section.test(line);
+ if (isParameter && currentSection) {
+ const match = line.match(regexPatterns.param);
+ const matchKey = match[1].toLowerCase();
+ const matchValue = match[2];
+ parsedIni[currentSection][matchKey] = valueHandler(matchKey, matchValue);
+ } else if (isSection) {
+ const match = line.match(regexPatterns.section);
+ const matchKey = match[1].toLowerCase();
+ parsedIni[matchKey] = {};
+ currentSection = matchKey;
+ }
+ });
+ return parsedIni;
+};
+
+export default iniParse;