Nutrient Packed Morsels

    function-sniffer
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.3 • Public • Published

    CircleCI Coverage Status NPM version download license

    Features

    • Sniffer function can or can't run.
    • can do lazy run until function is loaded.

    Environment Support

    • Modern browsers and Internet Explorer 6+
    IE / Edge
    IE / Edge
    Firefox
    Firefox
    Chrome
    Chrome
    Safari
    Safari
    Opera
    Opera
    Electron
    Electron
    IE6+, Edge last 2 versions last 2 versions last 2 versions last 2 versions last 2 versions

    Usage

    Using npm

    npm install --save function-sniffer

    node environment:

    var Sniffer = require('function-sniffer');

    webpack or similar environment:

    import Sniffer from 'function-sniffer';

    requirejs environment:

    requirejs(['node_modules/function-sniffer/dist/function-sniffer.cjs.js'], function (base) {
        // do something...
    })

    browser environment:

    <script src="node_modules/function-sniffer/dist/ifunction-sniffer.min.js"></script>

    Example

    This is a Object Tool has a function do. // tool.js

    (function(Tool, undefined){
        Tool.do = function(thing){
            console.log('try to do '+ thing);
        }
    })(window.Tool || (window.Tool = {});

    Now, Sniffer.run can execute function just like Tool.do.

    • Sniffer.run
      • base default: window, base is not required, if object is base on window.
      • name executebase.namefunction. is required.
      • prompt prompting when function can't be found and execute. not required.
      • showPromptFn custom prompt function. default is alert. not required.
      • subscribe if you want to lazy run function which is not exist now.Sniffer memory it.When function loaded, try Sniffer.trigger to run it. not required.
    import Sniffer from 'function-sniffer';
    Sniffer.run({
        name: 'Tool.do', 
        prompt: 'Tool.do is not exist.',
        showPromptFn: (str)=>{ console.log(str);}, 
        subscribe: true
    });

    Sniffer.trigger can run the memory list of function.

    • Sniffer.trigger
      • base default: window, base is not required, if object is base on window.
      • name executebase.namefunction. is required.
    import Sniffer from 'function-sniffer';
    Sniffer.run({
        name: 'Tool.walk', 
        subscribe: true
    });
     
    // walk() is not exist in Tool.And Sniffer memory it, Because `subscribe:true`.
     
    Tool.walk = function(person){
        console.log(person + ' is walking.');
    }
     
    // walk loaded.And trigger to run it.
     
    Sniffer.trigger({
        base: Tool,
        name: 'walk'
    }, 'leon')
     
    // => leon is walking.

    License

    MIT

    Install

    npm i function-sniffer

    DownloadsWeekly Downloads

    4

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    39.9 kB

    Total Files

    23

    Last publish

    Collaborators

    • wallxiaokai