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

snapdragon-position

v2.0.2

Published

Snapdragon util and plugin for patching the position on an AST node.

Downloads

22

Readme

snapdragon-position NPM version NPM monthly downloads NPM total downloads Linux Build Status

Snapdragon util and plugin for patching the position on an AST node.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save snapdragon-position

What does this do?

Adds a .position object to tokens that looks something like this:

{
  source: 'string',
  start: { index: 0, column: 1, line: 1 },
  end: { index: 3, column: 4, line: 1 },
  range: [0, 3] // getter
}

When used as snapdragon-lexer plugin, this adds a .position() method to the instance and patches the lexer.lex() and lexer.handle() methods to automatically add position objects to tokens.

There is a more detailed example below.

Heads up!

If you would prefer for the property name to be token.loc rather than token.position, use snapdragon-location instead.

API

The main export is a function that can be used as a plugin with snapdragon-lexer, or called directly with an instance of snapdragon-lexer.

position

Sets the start position and returns a function for setting the end position on a token.

Params

  • name {String|Object}: (optional) Snapdragon Lexer or Tokenizer instance, or the name to use for the position property on the token. Default is position.
  • target {Object}: Snapdragon Lexer or Tokenizer instance
  • returns {Function}: Returns a function that takes a token as its only argument

Example

const position = require('snapdragon-position');
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');

lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);

const pos = position(lexer);
const token = pos(lexer.advance());
console.log(token);

.plugin

Use as a plugin to add a .position method to your snapdragon-lexer or [snapdragon-tokenizer][] instance to automatically add a position object to tokens when the .lex() or .handle() methods are used.

Example

const Lexer = require('snapdragon-lexer');
const position = require('snapdragon-position');
const lexer = new Lexer();
lexer.use(position());

.location

Get the current source location, with index, column and line. Used by .position() to create the "start" and "end" locations.

  • returns {Object}: Returns an object with the current source location.

Example

const Lexer = require('snapdragon-lexer');
const lexer = new Lexer();
console.log(lexer.location());
//=> Location { index: 0, column: 0, line: 1 };

.position

Returns a function for getting the current position.

  • returns {Function}: Returns a function that takes a token as its only argument, and patches a .position property onto the token.

Example

const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');
lexer.use(position());

lexer.set('text', function(tok) {
  // get start position before advancing lexer
  const pos = this.position();
  const match = this.match(/^\w+/);
  if (match) {
    // get end position after advancing lexer (with .match)
    return pos(this.token(match));
  }
});

Params

  • start {Object}: (required) Starting location
  • end {Object}: (required) Ending location
  • target {Object}: (optional) Snapdragon Lexer or Tokenizer instance
  • returns {Object}

Example

const Lexer = require('snapdragon-lexer');
const Location = require('snapdragon-position').Location;
const lexer = new Lexer('foo/bar');
lexer.capture('text', /^\w+/);
lexer.advance();
console.log(new Location(lexer));
//=> Location { index: 3, column: 4, line: 1 }

Params

  • start {Object}: (required) Starting location
  • end {Object}: (required) Ending location
  • target {Object}: (optional) Snapdragon Lexer or Tokenizer instance
  • returns {Object}

Example

const Lexer = require('snapdragon-lexer');
const position = require('snapdragon-location');
const lexer = new Lexer('foo/bar')
  .capture('slash', /^\//)
  .capture('text', /^\w+/);

const start = new position.Location(lexer);
lexer.advance();
const end = new position.Location(lexer);
console.log(new position.Position(start, end, lexer));
// Position {
//   source: undefined,
//   start: Location { index: 0, column: 1, line: 1 },
//   end: Location { index: 3, column: 4, line: 1 } }

Plugin usage

When used as a plugin, this adds a .position() method to a snapdragon-lexer instance, for adding position information to tokens.

Example

const position = require('snapdragon-position');
const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/bar');
lexer.use(position());

lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);

var token = lexer.advance();
console.log(token);

Adds a .position object to the token, like this:

Token {
  type: 'text',
  value: 'foo',
  match: [ 'foo', index: 0, input: 'foo/*' ],
  position: {
    start: { index: 0, column: 1, line: 1 },
    end: { index: 3, column: 4, line: 1 },
    range: [0, 3] // getter
  } 
}

Token objects

See the Token documentation for more details about the Token object.

interface Token {
  type: string;
  value: string;
  match: array | undefined;
  position: Position;
}

Position objects

The token.position property contains source string position information on the token.

interface Position {
  source: string | undefined;
  start: Location;
  end: Location;
  range: array (getter)
}
  • source {string|undefined} - the source position provided by lexer.options.source. Typically this is a filename, but could also be string or any user defined value.
  • start {object} - start location object, which is the location of the first character of the lexed source string.
  • end {object} - end location object, which is the location of the last character of the lexed source string.
  • range {array} - getter that returns an array with the following values: [position.start.index, position.end.index]

Location objects

Each Location object consists of an index number (0-based), a column number (0-based), and a line number (1-based):

interface Location {
  index: number; // >= 0
  column: number; // >= 0,
  line: number; // >= 1
}
  • line {string|undefined} - the source position provided by lexer.options.source. Typically this is a filename, but could also be string or any user defined value.
  • column {object} - start location object, which is the location of the first character of the lexed source string.
  • end {object} - end location object, which is the location of the last character of the lexed source string.

Example usage

const Lexer = require('snapdragon-lexer');
const lexer = new Lexer('foo/*', { source: 'string' });
lexer.use(position());
lexer.capture('star', /^\*/);
lexer.capture('slash', /^\//);
lexer.capture('text', /^\w+/);

lexer.tokenize();
console.log(lexer.tokens);

Results in:

[
  {
    type: 'text',
    val: 'foo',
    match: ['foo', index: 0, input: 'foo/*'],
    position: {
      source: 'string',
      start: { index: 0, column: 1, line: 1 },
      end: { index: 3, column: 4, line: 1 },
      range: [0, 3]
    }
  },
  {
    type: 'slash',
    val: '/',
    match: ['/', index: 0, input: '/*'],
    position: {
      source: 'string',
      start: { index: 3, column: 4, line: 1 },
      end: { index: 4, column: 5, line: 1 },
      range: [3, 4]
    }
  },
  {
    type: 'star',
    val: '*',
    match: ['*', index: 0, input: '*'],
    position: {
      source: 'string',
      start: { index: 4, column: 5, line: 1 },
      end: { index: 5, column: 6, line: 1 },
      range: [4, 5]
    }
  }
]

About

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on January 08, 2018.