View on GitHub

Django-filters-js

A project to convert the default and contrib.humanize template filters from Django to JavaScript

Download this project as a .zip file Download this project as a tar.gz file

django-filters-js

A project to convert the default and contrib.humanize template filters from Django to JavaScript

Usage

Add <script src="django-filters.min.js"></script> to your page. There are no dependencies.

Chainable Strings

django.filter implements a chainable object similar to jQuery. This allows for multiple methods to be run in sequence. For example:

var myString    =   django.filter("This is a test sentence").slugify().cut('-'); // becomes "thisisatestsentence"
// or
var myString2   =   django.filter(200000);
myString2.apnumber().intcomma(); // becomes 200,000

Methods

In addition to the chainable pattern, you can call each method directly at django.filters.[methodName] with the string, number or date to filter as the first argument.

django.filters.slugify("This is a test sentence"); // becomes "this-is-a-test-sentence"
django.filters.slugify("This is a test sentence").cut('-'); // raises a TypeError

.intcomma()

Adds comma separators to a number or string representation of a number.

.apnumber()

For integers 0-9, returns the word. For integers 10+, returns the integer.

.slugify()

Returns a URI safe version of the string, lowercased with all non-standard characters replaced with '-'.

.ordinal()

Appends the ordinal suffix to a number or string representation of a number

.date(format)

Formats a Date object according to the format parameter (a string). Use % to escape characters; use %%to print a literal %. For formatting options, see Django's date documentation.

.time(format)

An alias of .date(). Unlike the Django implementation, .time() works with Date objects since there isn't a time-only equivalent in JavaScript.

.cut(toCut)

Removes all instances of the toCut parameter from the string.

.trim()

Removes whitespace from the beginning and end of the string. Only available on instances of django.filter(). To use on plain strings, see django.filters.utils.trim below.

Utility Methods

There are five methods primarily intended for internal use by the filter methods, but may be of use on their on as well.

django.filters.utils.trim(text)

Removes whitespace from the beginning and end of the string.

django.filters.utils.inArray(element, array)

Returns the position of element in array. Returns -1 if element is not found.

django.filters.utils.l_pad(object, length, pad)

Ensures that the string representation of object is at least as long as length. If object is shorter, pad is added to the beginning of object until is reaches length.

django.filters.utils.r_pad(object, length, pad)

Ensures that the string representation of object is at least as long as length. If object is shorter, pad is added to the end of object until is reaches length.

django.filters.utils.parseDate(dateString)

Turns a string representation of a date and turns it into a Date object. If the dateString cannot be parsed, returns the original string.