-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
>\\\< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |