Use create*
prefix to clarify curried functions
This commit is contained in:
parent
6e6b93d917
commit
8474f3cf7f
4 changed files with 25 additions and 23 deletions
|
@ -24,10 +24,10 @@ exports.ensureDirectory = async (userDataPath) => {
|
||||||
await fse.ensureDir(exports.getPath(userDataPath));
|
await fse.ensureDir(exports.getPath(userDataPath));
|
||||||
};
|
};
|
||||||
|
|
||||||
// readData :: AttachmentsPath ->
|
// createReader :: AttachmentsPath ->
|
||||||
// RelativePath ->
|
// RelativePath ->
|
||||||
// IO (Promise ArrayBuffer)
|
// IO (Promise ArrayBuffer)
|
||||||
exports.readData = (root) => {
|
exports.createReader = (root) => {
|
||||||
if (!isString(root)) {
|
if (!isString(root)) {
|
||||||
throw new TypeError('`root` must be a path');
|
throw new TypeError('`root` must be a path');
|
||||||
}
|
}
|
||||||
|
@ -43,10 +43,10 @@ exports.readData = (root) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// writeData :: AttachmentsPath ->
|
// createWriter :: AttachmentsPath ->
|
||||||
// ArrayBuffer ->
|
// ArrayBuffer ->
|
||||||
// IO (Promise RelativePath)
|
// IO (Promise RelativePath)
|
||||||
exports.writeData = (root) => {
|
exports.createWriter = (root) => {
|
||||||
if (!isString(root)) {
|
if (!isString(root)) {
|
||||||
throw new TypeError('`root` must be a path');
|
throw new TypeError('`root` must be a path');
|
||||||
}
|
}
|
||||||
|
@ -66,8 +66,10 @@ exports.writeData = (root) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// deleteData :: AttachmentsPath -> IO Unit
|
// createDeleter :: AttachmentsPath ->
|
||||||
exports.deleteData = (root) => {
|
// RelativePath ->
|
||||||
|
// IO Unit
|
||||||
|
exports.createDeleter = (root) => {
|
||||||
if (!isString(root)) {
|
if (!isString(root)) {
|
||||||
throw new TypeError('`root` must be a path');
|
throw new TypeError('`root` must be a path');
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,7 +127,7 @@ const _createMessage = ({ commonProperties, conversationId, type } = {}) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const FIXTURES_PATH = path.join(__dirname, '..', '..', 'fixtures');
|
const FIXTURES_PATH = path.join(__dirname, '..', '..', 'fixtures');
|
||||||
const readData = Attachments.readData(FIXTURES_PATH);
|
const readData = Attachments.createReader(FIXTURES_PATH);
|
||||||
const createRandomInMemoryAttachment = async () => {
|
const createRandomInMemoryAttachment = async () => {
|
||||||
const files = (await fs.readdir(FIXTURES_PATH)).map(createFileEntry);
|
const files = (await fs.readdir(FIXTURES_PATH)).map(createFileEntry);
|
||||||
const { contentType, fileName } = sample(files);
|
const { contentType, fileName } = sample(files);
|
||||||
|
|
|
@ -111,9 +111,9 @@ window.ProxyAgent = require('proxy-agent');
|
||||||
|
|
||||||
// ES2015+ modules
|
// ES2015+ modules
|
||||||
const attachmentsPath = Attachments.getPath(app.getPath('userData'));
|
const attachmentsPath = Attachments.getPath(app.getPath('userData'));
|
||||||
const deleteAttachmentData = Attachments.deleteData(attachmentsPath);
|
const deleteAttachmentData = Attachments.createDeleter(attachmentsPath);
|
||||||
const readAttachmentData = Attachments.readData(attachmentsPath);
|
const readAttachmentData = Attachments.createReader(attachmentsPath);
|
||||||
const writeAttachmentData = Attachments.writeData(attachmentsPath);
|
const writeAttachmentData = Attachments.createWriter(attachmentsPath);
|
||||||
|
|
||||||
// Injected context functions to keep `Message` agnostic from Electron:
|
// Injected context functions to keep `Message` agnostic from Electron:
|
||||||
const upgradeSchemaContext = {
|
const upgradeSchemaContext = {
|
||||||
|
|
|
@ -13,7 +13,7 @@ const NAME_LENGTH = 64;
|
||||||
const PATH_LENGTH = PREFIX_LENGTH + NUM_SEPARATORS + NAME_LENGTH;
|
const PATH_LENGTH = PREFIX_LENGTH + NUM_SEPARATORS + NAME_LENGTH;
|
||||||
|
|
||||||
describe('Attachments', () => {
|
describe('Attachments', () => {
|
||||||
describe('writeData', () => {
|
describe('createWriter', () => {
|
||||||
let tempRootDirectory = null;
|
let tempRootDirectory = null;
|
||||||
before(() => {
|
before(() => {
|
||||||
tempRootDirectory = tmp.dirSync().name;
|
tempRootDirectory = tmp.dirSync().name;
|
||||||
|
@ -25,9 +25,9 @@ describe('Attachments', () => {
|
||||||
|
|
||||||
it('should write file to disk and return path', async () => {
|
it('should write file to disk and return path', async () => {
|
||||||
const input = stringToArrayBuffer('test string');
|
const input = stringToArrayBuffer('test string');
|
||||||
const tempDirectory = path.join(tempRootDirectory, 'Attachments_writeData');
|
const tempDirectory = path.join(tempRootDirectory, 'Attachments_createWriter');
|
||||||
|
|
||||||
const outputPath = await Attachments.writeData(tempDirectory)(input);
|
const outputPath = await Attachments.createWriter(tempDirectory)(input);
|
||||||
const output = await fse.readFile(path.join(tempDirectory, outputPath));
|
const output = await fse.readFile(path.join(tempDirectory, outputPath));
|
||||||
|
|
||||||
assert.lengthOf(outputPath, PATH_LENGTH);
|
assert.lengthOf(outputPath, PATH_LENGTH);
|
||||||
|
@ -37,7 +37,7 @@ describe('Attachments', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('readData', () => {
|
describe('createReader', () => {
|
||||||
let tempRootDirectory = null;
|
let tempRootDirectory = null;
|
||||||
before(() => {
|
before(() => {
|
||||||
tempRootDirectory = tmp.dirSync().name;
|
tempRootDirectory = tmp.dirSync().name;
|
||||||
|
@ -48,7 +48,7 @@ describe('Attachments', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read file from disk', async () => {
|
it('should read file from disk', async () => {
|
||||||
const tempDirectory = path.join(tempRootDirectory, 'Attachments_readData');
|
const tempDirectory = path.join(tempRootDirectory, 'Attachments_createReader');
|
||||||
|
|
||||||
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
||||||
const fullPath = path.join(tempDirectory, relativePath);
|
const fullPath = path.join(tempDirectory, relativePath);
|
||||||
|
@ -57,13 +57,13 @@ describe('Attachments', () => {
|
||||||
const inputBuffer = Buffer.from(input);
|
const inputBuffer = Buffer.from(input);
|
||||||
await fse.ensureFile(fullPath);
|
await fse.ensureFile(fullPath);
|
||||||
await fse.writeFile(fullPath, inputBuffer);
|
await fse.writeFile(fullPath, inputBuffer);
|
||||||
const output = await Attachments.readData(tempDirectory)(relativePath);
|
const output = await Attachments.createReader(tempDirectory)(relativePath);
|
||||||
|
|
||||||
assert.deepEqual(input, output);
|
assert.deepEqual(input, output);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('deleteData', () => {
|
describe('createDeleter', () => {
|
||||||
let tempRootDirectory = null;
|
let tempRootDirectory = null;
|
||||||
before(() => {
|
before(() => {
|
||||||
tempRootDirectory = tmp.dirSync().name;
|
tempRootDirectory = tmp.dirSync().name;
|
||||||
|
@ -74,7 +74,7 @@ describe('Attachments', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should delete file from disk', async () => {
|
it('should delete file from disk', async () => {
|
||||||
const tempDirectory = path.join(tempRootDirectory, 'Attachments_deleteData');
|
const tempDirectory = path.join(tempRootDirectory, 'Attachments_createDeleter');
|
||||||
|
|
||||||
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
||||||
const fullPath = path.join(tempDirectory, relativePath);
|
const fullPath = path.join(tempDirectory, relativePath);
|
||||||
|
@ -83,7 +83,7 @@ describe('Attachments', () => {
|
||||||
const inputBuffer = Buffer.from(input);
|
const inputBuffer = Buffer.from(input);
|
||||||
await fse.ensureFile(fullPath);
|
await fse.ensureFile(fullPath);
|
||||||
await fse.writeFile(fullPath, inputBuffer);
|
await fse.writeFile(fullPath, inputBuffer);
|
||||||
await Attachments.deleteData(tempDirectory)(relativePath);
|
await Attachments.createDeleter(tempDirectory)(relativePath);
|
||||||
|
|
||||||
const existsFile = await fse.exists(fullPath);
|
const existsFile = await fse.exists(fullPath);
|
||||||
assert.isFalse(existsFile);
|
assert.isFalse(existsFile);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue