source: trunk/js/Utilities.js @ 622

Last change on this file since 622 was 590, checked in by anonymous, 7 years ago

Minor fixes. Increment version to 2.2.0-5.

File size: 5.1 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/*
24Emulates a sprintf function. Placeholders are {1}
{N}.
25Some versions of this function were zero-indexed; this one is not.
26---------------------------------------------------------------------
27"{1} is dead, but {2} is alive! {1} {3}".format("ASP", "ASP.NET")
28
29outputs
30
31ASP is dead, but ASP.NET is alive! ASP {3}
32---------------------------------------------------------------------
33*
34* @access   public
35* @param    string multiple Strings to pass to the formatted string.
36* @author   http://stackoverflow.com/a/4673436/277303
37* @version  1.0
38* @since    30 May 2014 18:02:39
39*/
40if (!String.prototype.format) {
41    String.prototype.format = function() {
42        var args = arguments;
43        return this.replace(/{(\d+)}/g, function(match, number) {
44            return typeof args[number-1] != 'undefined' ? args[number-1] : match;
45        });
46    };
47}
48
49/*
50* Displays 'user at domain dot com' as 'user@domain.com'.
51---------------------------------------------------------------------
52<span class="sc-email">user at domain dot com</span>
53<a href="mailto:user at domain dot com" class="sc-email">Email me</a>
54<script>
55$('.sc-email').nospam();
56</script>
57---------------------------------------------------------------------
58*
59* @access   public
60* @version  2.0
61* @since    30 Jun 2008 12:32:19
62*/
63jQuery.fn.nospam = function() {
64    return this.each(function(){
65        $(this).text($(this).text().replace(' at ', '@').replace(' dot ', '.'));
66        if (this.href) {
67            this.href = this.href.replace(' at ', '@').replace(' dot ', '.');
68        }
69    });
70};
71
72/*
73* Encode html entities by specific mapping table.
74* Decode HTML by proxying content via an in-memory div, setting its inner text which jQuery automatically encodes.
75Then we pull the encoded contents back out. The div never exists on the page.
76---------------------------------------------------------------------
77$('input').val(Strangecode.htmlEncode(string));
78---------------------------------------------------------------------
79
80@access   public
81@version  2.0
82@since    30 Jun 2013
83*/
84Strangecode.htmlEncode = function (str) {
85    var entityMap = {
86        '&': '&amp;',
87        '<': '&lt;',
88        '>': '&gt;',
89        '"': '&quot;',
90        "'": '&#39;',
91        '/': '&#x2F;',
92        '`': '&DiacriticalGrave;'
93    };
94    return String(str).replace(/[&<>"'\/`]/g, function (s) {
95        return entityMap[s];
96    });
97};
98Strangecode.htmlDecode = function (str) {
99    return $('<div>').html(str).text();
100};
101
102
103/*
104Returns a string with URL-unsafe characters removed.
105---------------------------------------------------------------------
106var urlslug = $('.url').val().slug();
107---------------------------------------------------------------------
108* @access   public
109* @version  1.0
110* @since    30 Jun 2013
111*/
112$.fn.slug = function() {
113    str = this.text().trim().toLowerCase();
114    var from = 'áéíóúàÚìòùÀëïöÌÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîÎûÂÊÎÔÛñçÇ@·/_,:;';
115    var to   = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa------';
116    for (var i=0, l=from.length; i<l; i++) {
117        str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
118    }
119    return str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-');
120};
121
122
123
124/*
125Remove rounding errors caused by representation of finite binary floating point numbers.
126---------------------------------------------------------------------
127> (.1*.2)
1280.020000000000000004
129> (.1*.2).trim()
1300.02
131---------------------------------------------------------------------
132* @access   public
133* @version  1.0
134* @since    24 Nov 2015
135*/
136if (!Number.prototype.trim) {
137    Number.prototype.trim = function (precision) {
138        var precision = precision || 11;
139        return Math.round(this * Math.pow(10, precision)) / Math.pow(10, precision);
140    };
141}
142
143/*
144Uppercase the first letter of string.
145---------------------------------------------------------------------
146> 'hello world'.trim()
147Hello world
148---------------------------------------------------------------------
149* @access   public
150* @version  1.0
151* @since    24 Nov 2015
152*/
153if (!String.prototype.ucfirst) {
154    String.prototype.ucfirst = function() {
155        return this.charAt(0).toUpperCase() + this.slice(1);
156    };
157}
158
Note: See TracBrowser for help on using the repository browser.