npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

random-line-access

v0.1.3

Published

Random access line

Downloads

4

Readme

random-line-access stability

npm version build status test coverage downloads js-standard-style

Provides random access to keyed lines of arbitrarily sized files.

If you have a file larger than memory that you want to retrieve rows from at random, and your file happens to be structured such that the first word of each line can be regarded as a key for the entire line, well, then this is what you've been looking for.

Usage

huge.csv

example1,1,2,3,4
example2,hamburger,rango,,,martians
somemore,ozone,zone,zoneout
"wow complex",margarine,bananas,,,robot

example.js

const randomLineAccess = require('random-line-access')

let rla = randomLineAccess('huge.csv', {sep: ','})

rla.ls(function(err, ls) {
  console.log(ls) // ['example1', 'example2', 'somemore', '"wow']
})

rla.get('example2', function(err, data) {
  console.log(data) // ['hamburger', 'rango', 'martians']
})

let rla = randomLineAccess('huge.csv', {sep: ','})
rla.get('example2', function(err, data) {
  console.log(data) // ['hamburger', 'rango', 'martians']
})


let rla = randomLineAccess('huge.csv')
rla.get('example1', function(err, data) {
  console.log(data) // '1,2,3,4'
})

let rla = randomLineAccess('huge.csv', {sep: ',', quotes: true})
rla.ls(function(err, ls) {
  console.log(ls) // ['example1', 'example2', 'somemore', 'wow complex']
})

rla.get('wow complex', function(err, data) {
  console.log(data) // ['margarine', 'bananas', 'robot']
})

rla.set('wow complex', ['molasses'], function(err, data) {
  console.log(data) // ['molasses']
})

rla.close()

API

randomLineAccess

  • randomLineAccess(filePath, opts) Careful: the set operator has write permission

    Options include:

    • omitEmpty Expects a boolean, defaults to true. Removes empty values.
    • quotes Expects a boolean, defaults to false. Accepts quotes in the key value (IS IGNORED IN SEPARATED VALUES)
    • sep Expects a string. When provided, returns values seperated by the separator, and inserts arrays joined by seperator.

    Returns an instance.

    But, if you are curious: what it does concretely is scan the provided file for line breaks, stores the offsets to each key, length of each line in a hash. On get, it uses these together to calculate and pluck the lines on request.

Instance

  • get(key, callback)

    Takes a key and returns the value retrieved to the callback.

  • set(key, buffer, callback) Careful: the set operator has write permission, and no undo

    Takes a key, and either a buffer or a string. If the resulting buffer from the string or buffer is longer than the line in the document then this will raise an error. Dynamic reassignment of sizes is not supported because the author did not need it, nor did the author know an easy way to implement non-destructive insertion and efficient offset updates.

  • ls(callback)

    Returns all keys found in the document. Remember, keys are assumed to be the first string in the beginning of each line in the document.

  • close(callback)

    Closes the file.

Installation

$ npm install random-line-access

License

MIT