Skip to content

Commit

Permalink
feat: improve the has function
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoDev1611 committed Aug 21, 2022
1 parent e40fe2d commit 7b9dd0f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
120 changes: 120 additions & 0 deletions 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { basename, extname, join } from 'path/mod.ts';
import { BenoSetTypes, BenoTypes } from './src/parsers/types.d.ts';
import { benoMagicReader, benoOneFile } from './src/parsers/reader.ts';
import { Validate } from './src/validator.ts';

// Methods for Parsers
interface BenoCfgFunctions {
config(path?: string, env?: string): void;
get(key: string): unknown | undefined;
content(): Record<string, unknown>[] | undefined;
set(filename: string, object: BenoSetTypes): boolean;
has(filename: string, key:string): boolean;
}

export class Beno implements BenoCfgFunctions {
version = '0.1.0';

constructor(private props: BenoTypes) {
this.props.path = join(Deno.cwd(), 'config');
this.props.envPath = join(Deno.cwd(), '.env');
}

config(path?: string | undefined, env?: string | undefined): void {
/** Make a validation */

// Helpers for the Params
let cfg, envPath;

// Add the
if (path == undefined) {
cfg = join(Deno.cwd(), 'config');
} else {
cfg = path;
}

if (env == undefined) {
envPath = join(Deno.cwd(), '.env');
} else {
envPath = env;
}

this.props.path = cfg!;
this.props.envPath = envPath!;
}

content(): Record<string, unknown>[] | undefined {
const path: string[] = [];
for (const e of Deno.readDirSync(this.props.path!)) {
if (e.isFile && extname(e.name).substring(1) == this.props.encoder) {
path.push(join(this.props.path!, e.name));
}
}
return benoMagicReader(path, this.props.encoder);
}

get(key: string): Validate {
const content = this.content();

if (content == undefined) {
throw new Error(
'Beno ERROR: Not defined the content not found a valid file in the directory or not found the directory',
);
}
const target = content.find((i) => key in i);
return new Validate((target ?? {})[key]);
}

set(filename: string, object: BenoSetTypes): boolean {
const content = this.content();

if (content == undefined) {
throw new Error(
'Beno ERROR: Not defined the content not found a valid file in the directory or not found the directory',
);
}

content.map((e) => {
if (extname(filename) != '') {
filename = filename.replace(`.${this.props.encoder}`, '');
}
if (typeof e.BENO_INTERNALS_FILEPATH == 'string') {
if (
filename ==
basename(e.BENO_INTERNALS_FILEPATH).replace(
`.${this.props.encoder}`,
'',
)
) {
const obj = benoOneFile(
e.BENO_INTERNALS_FILEPATH,
this.props.encoder,
);

obj[object.key] = object.val;

try {
Deno.writeTextFileSync(
e.BENO_INTERNALS_FILEPATH,
JSON.stringify(obj, null, 2),
);
} catch (e) {
throw new Error(
`Beno ERROR: Is not possible write the file error.\n${e}`,
);
}
return true;
}
} else {
throw new Error(
`Beno ERROR: Not valid internal path please report this on github maybe a Bug`,
);
}
});

return false;
}

has(filename: string, key: string): boolean {
}
}
7 changes: 5 additions & 2 deletions examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ const beno = new Beno({ encoder: 'json' });
/* console.log(beno.get('example').string());
console.log(beno.get('to').number());
console.log(beno.get('yes').boolean()); */
beno.set('other', { key: 'helloo', val: 'benooooooooo' });
beno.set('hello.json', { key: 'bye', val: 'chauuuuuuuu' });
// beno.set('other', { key: 'helloo', val: 'benooooooooo' });
// beno.set('hello.json', { key: 'bye', val: 'chauuuuuuuu' });

console.log(beno.has('helloo'));
console.log(beno.has('notexistidkmaybeyes'));

/* const JSONC = new Beno({encoder: 'jsonc'})
console.log(JSONC.content())
Expand Down
14 changes: 14 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface BenoCfgFunctions {
get(key: string): unknown | undefined;
content(): Record<string, unknown>[] | undefined;
set(filename: string, object: BenoSetTypes): boolean;
has(filename: string, key: string): boolean;
}

export class Beno implements BenoCfgFunctions {
Expand Down Expand Up @@ -113,4 +114,17 @@ export class Beno implements BenoCfgFunctions {

return false;
}

has(key: string): boolean {
const content = this.content();

if (content == undefined) {
throw new Error(
'Beno ERROR: Not defined the content not found a valid file in the directory or not found the directory',
);
}

if (content.find((i) => key in i) != undefined) return true;
return false;
}
}

0 comments on commit 7b9dd0f

Please sign in to comment.