sarah-lib-utils

1.1.3 • Public • Published

S.A.R.A.H. utils lib

Scrutinizer Code Quality Code Coverage Build Status

npm license

npm stat

Library with helpful class for S.A.R.A.H.

Installation

> npm install sarah-lib-utils --save

Usage

S.A.R.A.H. integration

/** your_plugin_name.js */

const sarahLibUtils = require('sarah-lib-utils');
const SarahActionContext = sarahLibUtils.SarahActionContext;
const SarahActionHelper = sarahLibUtils.SarahActionHelper;
const version = sarahLibUtils.version;

exports.action = function (data, callback, config, SARAH) {
    var context = new SarahActionContext(data, callback);
    var helper = new SarahActionHelper(context);
    if (version.isV3()) {
         context.setSARAH(SARAH); // For v3 compatibility
    }
    var monModule = new MyModule();
    monModule.process(helper);
};

Version

Provide helpful methods to deal with Sarah version

// Load
const version = require('sarah-lib-utils/version');
// Helper
var isV3 = version.isV3();
var isV4 = version.isV4();
var sarahVersion = version.get();
if (version.v3 == versionNumber) {
    ...
}
if (version.v4 == sarahVersionNumber) {
    ...
}

Logger

SarahLogger will automatically add the channel in front of the message string

// Load
const SarahLogger = require('sarah-lib-utils/logger');
// Instantiate
var logger = new SarahLogger('channel');
// Helper
var message = 'myMessage';
logger.debug(message); // Will output 'debug: [channel] myMessage'
logger.log(message);   // Will output 'log: [channel] myMessage'
logger.info(message);  // Will output 'info: [channel] myMessage'
logger.warn(message);  // Will output 'warn: [channel] myMessage'
logger.error(message); // Will output 'error: [channel] myMessage'

Action Context

SarahActionContext is a simple wrapper for action data

// Load
const SarahActionContext = require('sarah-lib-utils/actionContext');
// Instantiate
var context = new SarahActionContext(data, callback);
// Setter
context.setSARAH(sarah);
// Getter
context.getData();
context.getCallback();
context.getSARAH();

Action Helper

SarahActionHelper provide helpful methods regarding action for module which want to run in SARAH v3 AND v4

Basics

// Load
const SarahActionHelper = require('sarah-lib-utils/actionHelper');
// Instantiate
var helper = new SarahActionHelper(<SarahActionContext> actionContext);
// helper
helper.speak(tts);
// Getter
helper.getContext();

S.A.R.A.H. module integration

/** MyModule.js */
/**
 * @public
 *
 * @param {object}            data
 * @param {SarahActionHelper} helper
 */
MyModule.prototype.process = function (helper) {
    helper.speak('c\'est partis');
    /** @var {SarahActionContext} context */
    var context = helper.getContext();
    /** @var {object} data */
    var data = context.getData();
    /** @var {callable} callback */
    var callback = context.getCallback();
    /** @var {SARAH} sarah
    var sarah = context.getSARAH();
    
    if (data.action == 'action1') {
       ...
    }
    ...
};

Tests

> npm test

Coverage

> npm run coverage

Then open the coverage/index.js.html file in a browser

EsLint

> npm run eslint

Release History

  • 1.0.0 : Initial release
  • 1.1.0 : Readme badges
  • 1.1.1 : Fix npm version
  • 1.1.2 : Fix npm link
  • 1.1.3 : Fix npm version

Dependents (0)

Package Sidebar

Install

npm i sarah-lib-utils

Weekly Downloads

0

Version

1.1.3

License

GPL-3.0

Last publish

Collaborators

  • yoanm