Code Monkey home page Code Monkey logo

carbon-filter's Introduction

CarbonJS Filters / carbon-filter

The carbon-filter module provides a set of commonly used data filters. This means you can run your data through a set of filters after you've received user's input or before you insert data into your database. You can use it as a stand-alone module or most commonly with carbon-form module which filters form data automatically for you.

If you've ever worked with any Zend_Filter filter from the Zend Framework you will be familiar with it's sintax and if you haven't then scroll down to the "Usage" section and you'll be up-and-running in no time.

Installation

npm install carbon-filter [--save]

Usage

The carbon-filter module packs some of the most used filters which share the same interface. Each filter provides public access to the function filter which receives only one parameter and returns filtered data. Some filters have options so that you can customize it to your needs.

The function filter has the following prototype Filter.prototype.filter = function(value) and it is common to all filters. It has 1 parameter:

  • value - Input value that needs to be filtered.

Stand-alone

var Filter = require("carbon-filter");

var stripTags = new Filter.StripTags({
    allowedTags: ["div", "span", "h1", "br"]
});

var inputValue = "...";
var filteredValue = stripTags.filter(inputValue);

With carbon-form

var Form = require("carbon-form");
var Filter = require("carbon-filter");

var form = new Form();

form.addElements([
    new Form.Element.Text("name", {
        label: "Name",
        filters: [
            new Filter.StringTrim(),
            new Filter.StripTags({
                allowedTags: ["div", "span"],
                allowedAttributes: ["id", "class"]
            })
        ]
    });
]);

Filters

StringTrim

Trims the beginning and the end of a string.

StripTags

Strips HTML tags from the input. You can specify which tags or attributes you'd like to leave (if any).

Options

  • allowedTags [Array] - Tags from the input that you want to keep in the output.
  • allowedAttributes [Array] - Attributes of HTML tags from the input that you want to keep in the output.

Contributing

If you're willing to contribute to this project feel free to report issues, send pull request, write tests or simply contact me - Amir Ahmetovic

Licence

This software is available under the following licenses:

  • MIT

carbon-filter's People

Contributors

choxnox avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.