diff --git a/Package/JavaScript/README.md b/Package/JavaScript/README.md new file mode 100644 index 0000000..065029a --- /dev/null +++ b/Package/JavaScript/README.md @@ -0,0 +1,36 @@ +# Bellande Format JavaScript Example + +``` +// Example usage +const bellandeFormatter = new BellandeFormat(); + +// Parse a Bellande file +const parsedData = bellandeFormatter.parseBellande('path/to/your/file.bellande'); +console.log(parsedData); + +// Write data to a Bellande file +const dataToWrite = { key: 'value', list: [1, 2, 3] }; +bellandeFormatter.writeBellande(dataToWrite, 'path/to/output/file.bellande'); +``` + +## Website NPM +- https://www.npmjs.com/package/bellande_format + +### Installation +- `npm i bellande_format` + + +``` +Name: bellande_format +Version: 0.1.0 +Summary: File type Formats +Home-page: github.com/RonaldsonBellande/bellande_format +Author: Ronaldson Bellande +Author-email: ronaldsonbellande@gmail.com +License: GNU General Public License v3.0 +Requires: numpy +Required-by: +``` + +## License +This Algorithm or Models is distributed under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/), see [LICENSE](https://github.com/RonaldsonBellande/bellande_format/blob/main/LICENSE) and [NOTICE](https://github.com/RonaldsonBellande/bellande_format/blob/main/LICENSE) for more information. diff --git a/Package/JavaScript/package.json b/Package/JavaScript/package.json new file mode 100644 index 0000000..23764c0 --- /dev/null +++ b/Package/JavaScript/package.json @@ -0,0 +1,19 @@ +{ + "name": "bellande_format", + "version": "0.1.1", + "description": "File type Formats", + "main": "src/bellande_parser.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "github.com/RonaldsonBellande/bellande_format" + }, + "keywords": [ + "Bellande", + "Format" + ], + "author": "Ronaldson Bellande", + "license": "GNU General Public License v3.0" +} diff --git a/Package/JavaScript/src/bellande_parser.js b/Package/JavaScript/src/bellande_parser.js new file mode 100644 index 0000000..c86feb1 --- /dev/null +++ b/Package/JavaScript/src/bellande_parser.js @@ -0,0 +1,97 @@ +const fs = require('fs'); + +class BellandeFormat { + parseBellande(filePath) { + const content = fs.readFileSync(filePath, 'utf8'); + const lines = content.split('\n'); + return this.parseLines(lines); + } + + parseLines(lines) { + const result = {}; + const stack = [[-1, result]]; + + for (const line of lines) { + const stripped = line.trim(); + if (!stripped || stripped.startsWith('#')) continue; + + const indent = line.length - line.trimLeft().length; + while (stack.length && indent <= stack[stack.length - 1][0]) { + stack.pop(); + } + + const parent = stack[stack.length - 1][1]; + + if (stripped.includes(':')) { + const [key, value] = stripped.split(':').map(s => s.trim()); + if (value) { + parent[key] = this.parseValue(value); + } else { + const newDict = {}; + parent[key] = newDict; + stack.push([indent, newDict]); + } + } else if (stripped.startsWith('-')) { + const value = stripped.slice(1).trim(); + if (Array.isArray(parent)) { + parent.push(this.parseValue(value)); + } else { + const newList = [this.parseValue(value)]; + const lastKey = Object.keys(parent).pop(); + parent[lastKey] = newList; + stack.push([indent, newList]); + } + } + } + + return result; + } + + parseValue(value) { + if (value.toLowerCase() === 'true') return true; + if (value.toLowerCase() === 'false') return false; + if (value.toLowerCase() === 'null') return null; + if (value.startsWith('"') && value.endsWith('"')) return value.slice(1, -1); + if (/^-?\d+$/.test(value)) return parseInt(value, 10); + if (/^-?\d*\.\d+$/.test(value)) return parseFloat(value); + return value; + } + + writeBellande(data, filePath) { + const content = this.toBellandeString(data); + fs.writeFileSync(filePath, content); + } + + toBellandeString(data, indent = 0) { + if (typeof data === 'object' && data !== null) { + if (Array.isArray(data)) { + return data.map(item => `${' '.repeat(indent)}- ${this.toBellandeString(item, indent + 2)}`).join('\n'); + } else { + return Object.entries(data) + .map(([key, value]) => { + if (typeof value === 'object' && value !== null) { + return `${' '.repeat(indent)}${key}:\n${this.toBellandeString(value, indent + 2)}`; + } else { + return `${' '.repeat(indent)}${key}: ${this.formatValue(value)}`; + } + }) + .join('\n'); + } + } else { + return this.formatValue(data); + } + } + + formatValue(value) { + if (typeof value === 'string') { + return value.includes(' ') || value.includes(':') ? `"${value}"` : value; + } + if (typeof value === 'boolean') { + return value.toString(); + } + if (value === null) { + return 'null'; + } + return value.toString(); + } +} diff --git a/Package/Python/README.md b/Package/Python/README.md index ae3a3e4..c9cb265 100644 --- a/Package/Python/README.md +++ b/Package/Python/README.md @@ -1,4 +1,4 @@ -# Bellande Format Example +# Bellande Format Python Example ``` bellande_formatter = Bellande_Format()