source: trunk/js/Utilities.js @ 552

Last change on this file since 552 was 552, checked in by anonymous, 8 years ago

Updated js.

File size: 5.0 KB
Line 
1/*
2* The Strangecode Codebase - a general application development framework for PHP
3* For details visit the project site: <http://trac.strangecode.com/codebase/>
4* Copyright © 2014 Strangecode, LLC
5*
6* This program is free software: you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18*/
19
20// Codebase functions will be under the Strangecode namespace, unless they are added to the jQuery object for chaining.
21var Strangecode = Strangecode || {};
22
23/*
24* Emulates a sprintf function.
25---------------------------------------------------------------------
26"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")
27
28outputs
29
30ASP is dead, but ASP.NET is alive! ASP {2}
31---------------------------------------------------------------------
32*
33* @access   public
34* @param    string multiple Strings to pass to the formatted string.
35* @author   http://stackoverflow.com/a/4673436/277303
36* @version  1.0
37* @since    30 May 2014 18:02:39
38*/
39if (!String.prototype.format) {
40    String.prototype.format = function() {
41        var args = arguments;
42        return this.replace(/{(\d+)}/g, function(match, number) {
43            return typeof args[number-1] != 'undefined' ? args[number-1] : match;
44        });
45    };
46}
47
48/*
49* Displays 'user at domain dot com' as 'user@domain.com'.
50---------------------------------------------------------------------
51<span class="sc-email">user at domain dot com</span>
52<a href="mailto:user at domain dot com" class="sc-email">Email me</a>
53<script>
54$('.sc-email').nospam();
55</script>
56---------------------------------------------------------------------
57*
58* @access   public
59* @version  2.0
60* @since    30 Jun 2008 12:32:19
61*/
62jQuery.fn.nospam = function() {
63    return this.each(function(){
64        $(this).text($(this).text().replace(' at ', '@').replace(' dot ', '.'));
65        if (this.href) {
66            this.href = this.href.replace(' at ', '@').replace(' dot ', '.');
67        }
68    });
69};
70
71/*
72* Encode html entities by specific mapping table.
73* Decode HTML by proxying content via an in-memory div, setting its inner text which jQuery automatically encodes.
74Then we pull the encoded contents back out. The div never exists on the page.
75---------------------------------------------------------------------
76$('input').val(Strangecode.htmlEncode(string));
77---------------------------------------------------------------------
78
79@access   public
80@version  2.0
81@since    30 Jun 2013
82*/
83Strangecode.htmlEncode = function (str) {
84    var entityMap = {
85        '&': '&amp;',
86        '<': '&lt;',
87        '>': '&gt;',
88        '"': '&quot;',
89        "'": '&#39;',
90        '/': '&#x2F;',
91        '`': '&DiacriticalGrave;'
92    };
93    return String(str).replace(/[&<>"'\/`]/g, function (s) {
94        return entityMap[s];
95    });
96};
97Strangecode.htmlDecode = function (str) {
98    return $('<div>').html(str).text();
99};
100
101
102/*
103Returns a string with URL-unsafe characters removed.
104---------------------------------------------------------------------
105var urlslug = $('.url').val().slug();
106---------------------------------------------------------------------
107* @access   public
108* @version  1.0
109* @since    30 Jun 2013
110*/
111$.fn.slug = function() {
112    str = this.text().trim().toLowerCase();
113    var from = 'áéíóúàÚìòùÀëïöÌÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîÎûÂÊÎÔÛñçÇ@·/_,:;';
114    var to   = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa------';
115    for (var i=0, l=from.length; i<l; i++) {
116        str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
117    }
118    return str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-');
119};
120
121
122
123/*
124Remove rounding errors caused by representation of finite binary floating point numbers.
125---------------------------------------------------------------------
126> (.1*.2)
1270.020000000000000004
128> (.1*.2).trim()
1290.02
130---------------------------------------------------------------------
131* @access   public
132* @version  1.0
133* @since    24 Nov 2015
134*/
135if (!Number.prototype.trim) {
136    Number.prototype.trim = function (precision) {
137        var precision = precision || 11;
138        return Math.round(this * Math.pow(10, precision)) / Math.pow(10, precision);
139    };
140}
141
142/*
143Uppercase the first letter of string.
144---------------------------------------------------------------------
145> 'hello world'.trim()
146Hello world
147---------------------------------------------------------------------
148* @access   public
149* @version  1.0
150* @since    24 Nov 2015
151*/
152if (!String.prototype.ucfirst) {
153    String.prototype.ucfirst = function() {
154        return this.charAt(0).toUpperCase() + this.slice(1);
155    };
156}
157
Note: See TracBrowser for help on using the repository browser.