Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
itzmzn authored Mar 1, 2024
1 parent d59806f commit 2362d89
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/lowdb/adapters/CAF
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
>\\\<
7 changes: 7 additions & 0 deletions lib/lowdb/adapters/JSONFile.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Adapter } from '../Low.js';
export declare class JSONFile<T> implements Adapter<T> {
private adapter;
constructor(filename: string);
read(): Promise<T | null>;
write(obj: T): Promise<void>;
}
19 changes: 19 additions & 0 deletions lib/lowdb/adapters/JSONFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { TextFile } = require('./TextFile.js');
class JSONFile {
constructor(filename) {
this.adapter = new TextFile(filename);
}
async read() {
const data = await this.adapter.read();
if (data === null) {
return null;
}
else {
return JSON.parse(data);
}
}
write(obj) {
return this.adapter.write(JSON.stringify(obj, null, 2));
}
}
module.exports = { JSONFile };
7 changes: 7 additions & 0 deletions lib/lowdb/adapters/JSONFileSync.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SyncAdapter } from '../LowSync.js';
export declare class JSONFileSync<T> implements SyncAdapter<T> {
private adapter;
constructor(filename: string);
read(): T | null;
write(obj: T): void;
}
19 changes: 19 additions & 0 deletions lib/lowdb/adapters/JSONFileSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { TextFileSync } = require('./TextFileSync.js');
class JSONFileSync {
constructor(filename) {
this.adapter = new TextFileSync(filename);
}
read() {
const data = this.adapter.read();
if (data === null) {
return null;
}
else {
return JSON.parse(data);
}
}
write(obj) {
this.adapter.write(JSON.stringify(obj, null, 2));
}
}
module.exports = { JSONFileSync };
7 changes: 7 additions & 0 deletions lib/lowdb/adapters/LocalStorage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SyncAdapter } from '../LowSync.js';
export declare class LocalStorage<T> implements SyncAdapter<T> {
private key;
constructor(key: string);
read(): T | null;
write(obj: T): void;
}
16 changes: 16 additions & 0 deletions lib/lowdb/adapters/LocalStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class LocalStorage {
constructor(key) {
this.key = key;
}
read() {
const value = localStorage.getItem(this.key);
if (value === null) {
return null;
}
return JSON.parse(value);
}
write(obj) {
localStorage.setItem(this.key, JSON.stringify(obj));
}
}
module.exports = { LocalStorage };
6 changes: 6 additions & 0 deletions lib/lowdb/adapters/Memory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Adapter } from '../Low.js';
export declare class Memory<T> implements Adapter<T> {
private data;
read(): Promise<T | null>;
write(obj: T): Promise<void>;
}
13 changes: 13 additions & 0 deletions lib/lowdb/adapters/Memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Memory {
constructor() {
this.data = null;
}
read() {
return Promise.resolve(this.data);
}
write(obj) {
this.data = obj;
return Promise.resolve();
}
}
module.exports = { Memory };
6 changes: 6 additions & 0 deletions lib/lowdb/adapters/MemorySync.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SyncAdapter } from '../LowSync.js';
export declare class MemorySync<T> implements SyncAdapter<T> {
private data;
read(): T | null;
write(obj: T): void;
}
12 changes: 12 additions & 0 deletions lib/lowdb/adapters/MemorySync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class MemorySync {
constructor() {
this.data = null;
}
read() {
return this.data || null;
}
write(obj) {
this.data = obj;
}
}
module.exports = { MemorySync };
8 changes: 8 additions & 0 deletions lib/lowdb/adapters/TextFile.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Adapter } from '../Low.js';
export declare class TextFile implements Adapter<string> {
private filename;
private writer;
constructor(filename: string);
read(): Promise<string | null>;
write(str: string): Promise<void>;
}
25 changes: 25 additions & 0 deletions lib/lowdb/adapters/TextFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require('fs');
const { Writer } = require('steno');
class TextFile {
constructor(filename) {
this.filename = filename;
this.writer = new Writer(filename);
}
async read() {
let data;
try {
data = await fs.promises.readFile(this.filename, 'utf-8');
}
catch (e) {
if (e.code === 'ENOENT') {
return null;
}
throw e;
}
return data;
}
write(str) {
return this.writer.write(str);
}
}
module.exports = { TextFile };
8 changes: 8 additions & 0 deletions lib/lowdb/adapters/TextFileSync.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SyncAdapter } from '../LowSync.js';
export declare class TextFileSync implements SyncAdapter<string> {
private tempFilename;
private filename;
constructor(filename: string);
read(): string | null;
write(str: string): void;
}
26 changes: 26 additions & 0 deletions lib/lowdb/adapters/TextFileSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs');
const path = require('path');
class TextFileSync {
constructor(filename) {
this.filename = filename;
this.tempFilename = path.join(path.dirname(filename), `.${path.basename(filename)}.tmp`);
}
read() {
let data;
try {
data = fs.readFileSync(this.filename, 'utf-8');
}
catch (e) {
if (e.code === 'ENOENT') {
return null;
}
throw e;
}
return data;
}
write(str) {
fs.writeFileSync(this.tempFilename, str);
fs.renameSync(this.tempFilename, this.filename);
}
}
module.exports = { TextFileSync };

0 comments on commit 2362d89

Please sign in to comment.