xtractor

0.20.0 • Public • Published

xtractor 🚜 Build Status npm version

Simple string extraction

xtractor statically analyzes js files or globs of files and extracts strings within designated function call

Usage

Import the library

var xtractor = require('xtractor');

or

import * as xtractor from 'xtractor';

Extract strings from a file

xtractor.extractFromFile(__dirname + '/source.js', ['_', 'i18n._'], function(err, strings) {
    // use strings
});

A promise-based API is also supported

xtractor.extractFromFile(__dirname + '/source.js', ['_', 'i18n._'])
.then(function(strings) {
    // use strings
})
.catch(function(err) {
    // process error
});

Extract strings from a glob

xtractor.extractGlob(__dirname + '/*.js', ['_', 'i18n._'], function(err, strings) {
    // use strings
});

Promised-based API

xtractor.extractGlob(__dirname + '/*.js', ['_', 'i18n._'])
.then(function(err, strings) {
    // use strings
})
.catch(function(err) {
    // process error
});

Specify an object with the glob as the glob property and a regexp ignore pattern as the ignorePattern property

xtractor.extractGlob({
    glob: __dirname + '/**/*.js',
    ignorePattern: /node_modules/
}, ['_', 'i18n._'], function(err, strings) {
    // use strings
});

Expected and output format

The expected format for extraction is a call to the marker function with either one or three arguments, depending on pluralization.

Suppose the marker function is i18n._():

Singular

const str = i18n._('You have new messages');

will output

{
    "msgid": "You have new messages",
    "loc": [
        {
            "filepath": "sample.js",
            "line": 1
        }
    ]
}

Plural

const str = i18n._('You have {{ count }} new message', 'You have {{ count }} new messages', count);

will output

{
    "msgid": [
        "You have {{ count }} new message",
        "You have {{ count }} new messages"
    ],
    "loc": [
        {
            "filepath": "sample.js",
            "line": 1
        }
    ]
}

Readme

Keywords

none

Package Sidebar

Install

npm i xtractor

Weekly Downloads

1

Version

0.20.0

License

MIT

Last publish

Collaborators

  • arthuralee