Skip to content

Commit

Permalink
Added JSONLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Jan 8, 2024
1 parent caf84b3 commit 725c37c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
84 changes: 84 additions & 0 deletions src/ts/class/JSONLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// class/JSONLoader.ts

// Copyright 2023 Scape Agency BV

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


// ============================================================================
// Import
// ============================================================================

import { promises as fs } from 'fs';
import path from 'path';


// ============================================================================
// Classes
// ============================================================================

class JSONLoader {
/**
* Asynchronously loads JSON data from a file and returns it as an object.
* @param filePath The path to the JSON file.
* @returns A promise that resolves to an object containing the JSON data.
*/
async loadJSON<T>(filePath: string): Promise<T> {
try {
const data = await fs.readFile(filePath, 'utf8');
return JSON.parse(data) as T;
} catch (error) {
console.error(`Error reading JSON file: ${filePath}`, error);
throw error;
}
}

/**
* Asynchronously loads all JSON files from a given directory.
* @param dirPath The path to the directory containing JSON files.
* @returns A promise that resolves to an array of objects containing the JSON data.
*/
async loadJSONFromDirectory<T>(dirPath: string): Promise<T[]> {
try {
const files = await fs.readdir(dirPath);
const jsonFiles = files.filter(file => file.endsWith('.json'));

const jsonData = await Promise.all(
jsonFiles.map(file =>
this.loadJSON<T>(path.join(dirPath, file))
)
);

return jsonData;
} catch (error) {
console.error(`Error reading JSON files from directory: ${dirPath}`, error);
throw error;
}
}

/**
* Merges an array of objects into a single object.
* @param objects An array of objects to merge.
* @returns A single object containing all properties from the input objects.
*/
async mergeJSONObjects<T>(objects: T[]): Promise<T> {
return objects.reduce((acc, obj) => ({ ...acc, ...obj }), {} as T);
}
}


// ============================================================================
// Export
// ============================================================================

export default JSONLoader;
17 changes: 9 additions & 8 deletions src/ts/config/fantasticon.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import RunnerOptionalOptions from 'fantasticon';
const fantasticonConfig: any = {

// RunnerOptionalOptions
name: 'icon.gl',
name: 'icon',
fontTypes: [
FontAssetType.TTF, // TTF = "ttf"
FontAssetType.WOFF, // WOFF = "woff"
Expand Down Expand Up @@ -72,17 +72,18 @@ const fantasticonConfig: any = {
woff: './dist/font/icon.gl.woff',
woff2: './dist/font/icon.gl.woff2',
},
// codepoints: {
// 'chevron-left': 57344, // decimal representation of 0xe000
// 'chevron-right': 57345,
// 'thumbs-up': 57358,
// 'thumbs-down': 57359,
// },

codepoints: {
'chevron-left': 57344, // decimal representation of 0xe000
'chevron-right': 57345,
'thumbs-up': 57358,
'thumbs-down': 57359,
},
// fontHeight: number;
// descent: number;
// normalize: boolean;
// round: number;
selector: '.igl',
selector: '.i',
// tag: string;
// Use our custom Handlebars templates
// templates: {
Expand Down
2 changes: 2 additions & 0 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import SvgPackager from "./class/SvgPackager.js";
import TestRunner from './class/TestRunner.js';
import DocumentationGenerator from './class/DocumentationGenerator.js';
import CodeLinter from './class/CodeLinter.js';
import JSONLoader from './class/JSONLoader.js';


// Import | Internal Functions
Expand Down Expand Up @@ -87,6 +88,7 @@ export {
TestRunner,
DocumentationGenerator,
CodeLinter,
JSONLoader,

SvgReader,
SvgToPngConverter,
Expand Down

0 comments on commit 725c37c

Please sign in to comment.