Skip to content

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

Notifications You must be signed in to change notification settings

oddbird/django-filters-js

 
 

Repository files navigation

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

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

  • 1000000 becomes 1,000,000
  • 1000 becomes 1,000
  • 100 becomes 100

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

  • 1 becomes one
  • 10 becomes 10

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

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

  • 1 becomes 1st
  • 33 becomes 33rd

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.

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

Removes all instances of the toCut parameter from the string.

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.

Removes whitespace from the beginning and end of the string.

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

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.

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.

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 93.5%
  • CSS 5.2%
  • HTML 1.3%