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

file-pluck

v0.6.5

Published

Pluck snippets from files (or strings). Return strings or key/value pairs.

Downloads

30

Readme

File Pluck

Build Status Code Climate

  • Pluck snippets from files (or any plain string)
  • Return snippets in the form of strings or...
  • Break snippets into key/value pairs
  • Optionally, output a file in JSON format

File Pluck exposes an ES2015 promise API. Use it with your transpiler of choice, such as Babel, or any environment that supports native JavaScript promises.
Note: Node.js v4.2.3 supports promises out of the box. No need for a transpiler.

Install

npm i file-pluck

Usage

The pluckFile function returns a promise that will resolve to an array of "snippets" found within the target file(s). It accepts one argument—a single file as a string, or an array of files. Globs work too. Use it like so...

Assuming a file called example.txt contained the following...

...blah blah blah

/*** pluck this text ***/

blah blah blah...

/*** pluck this text too ***/

blah...

By default, opening and closing delimiters are set as /*** and ***/. You can pluck the snippets from example.txt like so...

'use strict';

let filePluck = require('file-pluck');

let p = filePluck();

let getSnippets = p.pluckFile('example.txt')

getSnippets.then(snippets => {
  console.log(snippets);
});

// ['pluck this text', 'pluck this text too']

Use custom delimiters on the following, index.html...

<div>

  <!-- <img src="img1" /> -->

  <div></div>

  <!-- <img src="img2" /> -->

</div>
let p = filePluck({
  opening: '<!--',
  closing: '-->'
});

let getSnippets = p.pluckFile('index.html');

getSnippets.then(snippets => {
  console.log(snippets);
});

// ['<img src="img1" />', '<img src="img2" />']

If you want to break down snippets into key/value pairs, you can do so. This requires delimiters on key/values. By default, they are { and }. Take the following file, main.css...


/***

name { base }
desc { Used for everything. }
example { <div class="base"></div> }

***/

.base {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/***

name { another-class }
desc { Not used for everything. }
example { <div class="another-class"></div> }

***/

.another-class {
  margin: 2rem;
}

You can create key/value pairs like so...

'use strict';

let filePluck = require('file-pluck');

let p = filePluck();

p.pluckFile('main.css')
.then(snippets => {
  console.log( p.objectify(snippets) );
});

// [{name: 'base', desc: 'Used for everything.', example: '<div class="base"></div>'}, {name: 'another-class', desc: 'Not used for everything.', example: '<div class="another-class"></div>'}]

Or write that data to a JSON file...

'use strict';

let filePluck = require('file-pluck');

let p = filePluck();

p.pluckFile('main.css')
.then(snippets => {
  p.writeJSON( 'output.json', p.objectify(snippets) );
});

Which would yeild the file output.json...

[
  {
    "name": "base",
    "desc": "Used for everything.",
    "example": "<div class=\"base\"></div>"
  },
  {
    "name": "another-class",
    " desc": "Not used for everything.",
    "example": "<div class=\"another-class\"></div>"
  }
]

API

filePluck([options])

options.opening
Type: string
Default: '/***'

options.closing
Type: string
Default: '***/'

options.valueOpening
Type: string
Default: '{'

options.valueClosing
Type: string
Default: '}'

filePluck.pluckable(string) boolean
Returns true if the input string contains opening and closing delimiters.

filePluck.pluck(string, [limit]) array
Returns an array of strings plucked from input string. Optionally, accepts a limit argument to limit the number of items in the returned array.

filePluck.pluckFile(file) promise.then(array => {})
Returns a promise that resolves to an array of strings. file can be a string value or an array of string values. Globs work, so feel free to mix and match as necessary, e.g. pluckFile(['*.html', 'main.css'])

filePluck.objectify(array) array
Maps an input array to an array of JS objects (key/value pairs).

filePluck.writeJSON(file, object) promise.then(object => {})
Writes a javascript object to a json file, returns a promise that resolves to the input object.