0.1.3 • Published 3 years ago
@standard-numbers/isbn-ranges v0.1.3
ISBN ranges
This Node.js module allows to download the latest ISBN range message from the International ISBN Agency website and convert the data to the JSON format. Visit https://www.isbn-international.org/range_file_generation/ to see details.
License
This software is available under the MIT license. See LICENSE.md
for the full license text.
Installation
npm install --ignore-scripts -- @standard-numbers/isbn-ranges
Usage
Add "type": "module"
to package.json
.
TypeScript ~4
(using top-level await
)
npm install --ignore-scripts --save-dev -- typescript@4 @types/node
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2017"
}
}
import {
areEqual,
getLatestMessage,
JSONValue,
} from '@standard-numbers/isbn-ranges';
import { readFile, writeFile } from 'fs/promises';
// Download and parse the latest ISBN range message.
const latestMessage = await getLatestMessage();
const path = 'isbn-ranges.json';
try {
// Get the previous message from the local filesystem.
const data = await readFile(path, {
encoding: 'utf8',
});
const oldMessage = JSON.parse(data) as JSONValue;
// Compare. The first argument of the `equal` function must be an array!
if (await areEqual([oldMessage, latestMessage])) {
// There is no need to update the local file.
} else {
// Update the local file.
await writeFile(path, JSON.stringify(latestMessage));
}
} catch (error) {
// The local file is not found. Create a new local file.
await writeFile(path, JSON.stringify(latestMessage));
}
TypeScript ~3.8.2
npm install --ignore-scripts --save-dev -- typescript@~3.8.2 @types/node
{
"compilerOptions": {
"module": "ES6",
"moduleResolution": "Node"
}
}
import {
areEqual,
getLatestMessage,
JSONValue,
} from '@standard-numbers/isbn-ranges';
import { readFile, writeFile } from 'fs/promises';
// Download and parse the latest ISBN range message.
getLatestMessage().then((latestMessage: JSONValue) => {
const path = 'isbn-ranges.json';
// Get the previous message from the local filesystem.
readFile(path, {
encoding: 'utf8',
}).then((data) => {
const oldMessage = JSON.parse(data) as JSONValue;
// Compare. The first argument of the `equal` function must be an array!
areEqual([oldMessage, latestMessage]).then(async (equal) => {
if (equal) {
// There is no need to update the local file.
} else {
// Update the local file.
await writeFile(path, JSON.stringify(latestMessage));
}
}).catch(() => {
// Could not compare the two messages.
});
}).catch(async () => {
// The local file is not found. Create a new local file.
await writeFile(path, JSON.stringify(latestMessage));
});
}).catch(() => {
// Failed to download or parse the latest ISBN range message.
});
JavaScript
import {
areEqual,
getLatestMessage,
} from '@standard-numbers/isbn-ranges';
import { readFile, writeFile } from 'fs/promises';
// Download and parse the latest ISBN range message.
getLatestMessage().then((latestMessage) => {
const path = 'isbn-ranges.json';
// Get the previous message from the local filesystem.
readFile(path, {
encoding: 'utf8',
}).then((data) => {
const oldMessage = JSON.parse(data);
// Compare. The first argument of the `equal` function must be an array!
areEqual([oldMessage, latestMessage]).then(async (equal) => {
if (equal) {
// There is no need to update the local file.
} else {
// Update the local file.
await writeFile(path, JSON.stringify(latestMessage));
}
}).catch(() => {
// Could not compare the two messages.
});
}).catch(async () => {
// The local file is not found. Create a new local file.
await writeFile(path, JSON.stringify(latestMessage));
});
}).catch(() => {
// Failed to download or parse the latest ISBN range message.
});