Disclaimer: This was a project I started for my personal entertainment and education. I have taken steps to make sure that it functions as expected, but I am not all-knowing. Use at your own risk!
This is a TS package I wrote whilst studying, and trying to replicate, the algorithm behind the Library of Babel website. The original creator, librarianofbabel, explains the approach and I followed this implementation very closely.
npm i --save @nitsudgo/library-of-babel
If you aren't familiar with how the Library of Babel works (from the short story), then this whole repo may not make much sense. I highly suggest you give it a read. The documentation will assume you have some knowledge of the concepts.
This package exports a LibraryOfBabel
class. First, you must import the package and create a new instance. Here, you may pass in any number to use as a seed. If no seed is given, a default value of 8888
will be used.
Instances that have different seeds will not have reproducible results between them.
const { LibraryOfBabel } = require('library-of-babel');
// If constructor is left blank, a default seed of 8888 is used
const LoB = new LibraryOfBabel(69);
The search function will look for either a partial or exact match of a given string in the library and return the address
of the page that the search result is in.
const searchString = 'some string to search for';
// Exact search looks for PRECISELY the string you have given
const exactSearchResult = LoB.search(searchString, 'exact');
// Partial search simply finds a page where your string appears in the content, regardless of where in the content it is located
const partialSearchResult = LoB.search(searchString, 'partial');
The getPage function accepts an address
and returns the content of that particular page.
const pageContent = LoB.getPage(exactSearchResult);
The getTitle function accepts an address
and returns the title of the volume (book). Different pages within the same volume will give the same result.
const pageTitle = LoB.getTitle(exactSearchResult);
To play with the code, simply clone
this Git repo to your machine, cd
into the project directory root, and run the following:
npm i
I've included some test suites in the /__tests__
directory. To run them, simply enter the following command in the terminal.
npm run test
To contribute, simply checkout to your own branch, push your commits to that branch, and then raise a pull request!
I hope you enjoy the package!