critical.js – View

web developers

critical.js (crit) is a JavaScript library containing methods added to the default objects which i deemed important, and difficult to create quality projects without. Most of these methods i coded myself, but a couple i borrowed from elsewhere on the net. These are marked with an asterisk (*) linking to the original source. Some i use myself, such as sreverse, others, such as map, not so much. Whether i use them or not, i hope you find them useful. Even if you don't want to use some of them, please don't remove them, as some other crit methods may reply on them. All functions are in the crit namespace, for example: crit.sreverse('abc')

critical.js is an open-source project, licensed under the MIT license. Click here for the full text of the license.

To use critical.js, download critical-min.js and link to it (in the HEAD section of your HTML file, before any other code) like this: <script type="text/javascript" src="/critical-min.js"></script>. You can also get the non-minified here. A full documentation (using JSDoc syntax) is in the source.

There are 19 functions as of critical.js 0.5 (the current beta). They are as follows:

  • typeOf*
  • Is a replacement for the native typeof function, as it doesn't support arrays or null.
  • clone*
  • Clones an object, instead of leaving a reference to it.
  • allBut
  • Returns all but a certain index of an array or string as a new object.
  • map
  • Runs a function on each item of an array, and returns a new object.
  • count
  • Counts the occurrence of a value in an array or string.
  • nthIndexOf
  • Works similar to the indexOf method of strings and arrays, except this function finds the nth index of a value, not only the first.
  • arandom
  • Returns a random item from an array.
  • scramble*
  • Scrambles an array or string, and returns a new object.
  • print
  • Prints an array in an easily read format.
  • sreverse
  • Returns a new string, the reverse of the one supplied.
  • srepeat
  • Returns a new string that is the original repeated.
  • strim
  • Returns a new string without surrounding whitespace.
  • nl2br
  • Replaces newlines with HTML linebreaks (<br/>).
  • btoBin
  • The binary value of a Boolean.
  • dateGetTimezone
  • Returns a number with the timezone offset from UTC in hours.
  • dateGetFullMonthName
  • Returns the full month name.
  • dateGetShortMonthName
  • Returns the short month name.
  • dateGetFullDayName
  • Returns the full day name.
  • dateGetShortDayName
  • Returns the short day name.