Wednesday, 29 May 2013

jQuery.log

To avoid checks if window.console is available:

  1. /****************************************
  2. * jQuery extensions
  3. ****************************************/
  4. jQuery.extend({
  5. "log": window.console ? window.console : { _isStubObject: true }
  6. });
  7. if (!jQuery.log._init) {
  8. jQuery.log._init = function (logLevel) {
  9. if (!jQuery.log._logLevels) {
  10. jQuery.log._logLevels = {
  11. All: 0,
  12. Debug: 1,
  13. Info: 2,
  14. Warn: 3,
  15. Error: 4
  16. };
  17. }
  18. if (!logLevel) logLevel = jQuery.log._logLevels.All;
  19.  
  20. if (!jQuery.log._unsupported_message) jQuery.log._unsupported_message = function (functionName) {
  21. if (window && window.console) {
  22. if (window.console.warn)
  23. window.console.warn("console." + functionName + "() is not supported!");
  24. else if (window.console.log)
  25. window.console.log("WARNING: console." + functionName + "() is not supported!");
  26. }
  27. };
  28.  
  29. if (!jQuery.log.memory) jQuery.log.memory = { _isStubObject: true };
  30. if (!jQuery.log.memory.jsHeapSizeLimit) jQuery.log.memory.jsHeapSizeLimit = -1;
  31. if (!jQuery.log.memory.totalJSHeapSize) jQuery.log.memory.totalJSHeapSize = -1;
  32. if (!jQuery.log.memory.usedJSHeapSize) jQuery.log.memory.usedJSHeapSize = -1;
  33.  
  34. if (!jQuery.log.assert) jQuery.log.assert = function () { jQuery.log._unsupported_message("assert"); };
  35. if (!jQuery.log.clear) jQuery.log.clear = function () { jQuery.log._unsupported_message("clear"); };
  36. if (!jQuery.log.count) jQuery.log.count = function () { jQuery.log._unsupported_message("count"); };
  37. if (!jQuery.log.debug) {
  38. jQuery.log._unsupported_message("debug");
  39. jQuery.log.debug = window.console.log;
  40. };
  41. if (!jQuery.log.dir) jQuery.log.dir = function () { jQuery.log._unsupported_message("dir"); };
  42. if (!jQuery.log.dirxml) jQuery.log.dirxml = function () { jQuery.log._unsupported_message("dirxml"); };
  43. if (!jQuery.log.error) {
  44. jQuery.log._unsupported_message("error");
  45. jQuery.log.error = window.console.log;
  46. };
  47. if (!jQuery.log.exception) jQuery.log.exception = function () { jQuery.log._unsupported_message("exception"); };
  48. if (!jQuery.log.group) jQuery.log.group = function () { jQuery.log._unsupported_message("group"); };
  49. if (!jQuery.log.groupCollapsed) jQuery.log.groupCollapsed = function () { jQuery.log._unsupported_message("groupCollapsed"); };
  50. if (!jQuery.log.groupEnd) jQuery.log.groupEnd = function () { jQuery.log._unsupported_message("groupEnd"); };
  51. if (!jQuery.log.info) {
  52. jQuery.log._unsupported_message("info");
  53. jQuery.log.info = window.console.log;
  54. };
  55. //if (!jQuery.log.log) jQuery.log.log = function () { jQuery.log._unsupported_message("log"); };
  56. if (!jQuery.log.markTimeline) jQuery.log.markTimeline = function () { jQuery.log._unsupported_message("markTimeline"); };
  57. if (!jQuery.log.profile) jQuery.log.profile = function () { jQuery.log._unsupported_message("profile"); };
  58. if (!jQuery.log.profileEnd) jQuery.log.profileEnd = function () { jQuery.log._unsupported_message("profileEnd"); };
  59. if (!jQuery.log.table) jQuery.log.table = function () { jQuery.log._unsupported_message("table"); };
  60. if (!jQuery.log.time) jQuery.log.time = function () { jQuery.log._unsupported_message("time"); };
  61. if (!jQuery.log.timeEnd) jQuery.log.timeEnd = function () { jQuery.log._unsupported_message("timeEnd"); };
  62. if (!jQuery.log.timeStamp) jQuery.log.timeStamp = function () { jQuery.log._unsupported_message("timeStamp"); };
  63. if (!jQuery.log.trace) jQuery.log.trace = function () { jQuery.log._unsupported_message("trace"); };
  64. if (!jQuery.log.warn) {
  65. jQuery.log._unsupported_message("warn");
  66. jQuery.log.warn = window.console.log;
  67. };
  68.  
  69. jQuery.log.info("INFO: jQuery.log is initialized.");
  70. };
  71. }
  72. jQuery.log._init();

JavaScript - String Extensions #2 [ replaceAll() ]

Unfortunately function replace() in JavaScript replaces only the first matching SubString.
Here is an extension of String Type, which allows you to replace all the found SubStrings.
You can optionally specify more than one (array of strings) string to search for.
  1. /****************************************
  2. * String extensions
  3. ****************************************/
  4. String.prototype.replaceAll = function (search, replace) {
  5. ///
  6. /// Replaces all instances of [search] String or Array of Strings
  7. ///
  8.  
  9. var searches = [];
  10. if (Object.prototype.toString.call(search) === '[object Array]') {
  11. searches = search;
  12. } else {
  13. searches = [search];
  14. }
  15. var result = this + "";
  16. for (var i = 0; i < searches.length; i++) {
  17. result = result.split(searches[i]).join(replace);
  18. }
  19.  
  20. return result;
  21. };

Usage of simple replace:
  1. var someString = "aaa-bbb-ccc-ddd";
  2. var resultCsv = someString.replaceAll("-", ";");

Replace array of strings:
  1. var someString = "aaa-bbb#ccc:ddd";
  2. var resultCsv = someString.replaceAll(["-", "#", ":"], ";");

JavaScript - String Extensions #1 [ isEmpty(), left(), right(), contains(), repeat() ]

Often, when I am working with Strings in JavaScript, I need many common functions.
Such as the left(), right(), isEmpty(), contains(), ETC.
To make work easier, I wrote a few to appropriate extensions for String:
  1. /****************************************
  2. * String extensions
  3. ****************************************/
  4. String.prototype.isEmpty = function () {
  5. ///
  6. /// Check if string is empty
  7. ///
  8. return (this.length === 0);
  9. };
  10.  
  11. String.prototype.left = function (subStringLength) {
  12. ///
  13. /// Gets left part of string
  14. ///
  15. if (subStringLength > this.length) return this;
  16. return this.substring(0, subStringLength);
  17. };
  18.  
  19. String.prototype.right = function (subStringLength) {
  20. ///
  21. /// Gets right part of string
  22. ///
  23. if (subStringLength > this.length) return this;
  24. return this.substring(this.length, this.length - subStringLength);
  25. };
  26.  
  27. String.prototype.contains = function (subString) {
  28. ///
  29. /// Checks if strin contains other string
  30. ///
  31. if (this.indexOf(subString) > 0) return true;
  32. return false;
  33. };
  34.  
  35. String.prototype.repeat = function (num) {
  36. ///
  37. /// Repeates string [num]-times
  38. ///
  39. if (isNaN(num)) return null;
  40. if (!(num > 0)) return "";
  41. return new Array(num + 1).join(this);
  42. };

MSSQL - Convert list to table #1 (array of numbers in string form)

Extended version of function from this post: MSSQL - Convert list to table #2 (array of string items)
Function described here (TABLE-Value UDF) converts a string in the form "123;bbb;;;456" to tabular form.
Result will contain only non-empty items which was possible to convert to INTEGER.
In case if list has non-unique items, the function returns the serial number for each items group (RANK).

  1. -- Drop Function if exists
  2. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.fnArray2IntTable'))
  3. DROP FUNCTION dbo.fnArray2IntTable;
  4. GO
  5.  
  6. /*=============================================
  7. Author:
  8. Yuri Abele
  9. Changes:
  10. 11.06.2013 - Yuri Abele - initial
  11. Description:
  12. Function to convert list of numeric Values to Table of integers
  13. Remark:
  14. Empty and non-numeric values will be ignored
  15.  
  16. Usage: Get all items
  17. SELECT * FROM dbo.fnArray2IntTable(N';444;bbb;333;;;333;444;555;', N';');
  18. Usage: Get all non-empty items and filter non-unique
  19. SELECT * FROM dbo.fnArray2IntTable(N';444;bbb;333;;;333;444;555;', N';') WHERE ItemRank=1;
  20. =============================================*/
  21. CREATE FUNCTION dbo.fnArray2IntTable(
  22. @Array NVARCHAR(MAX),
  23. @Delim NCHAR(1)
  24. )
  25. -- Container for Array Items
  26. RETURNS @Data TABLE(
  27. ItemIndex INT IDENTITY(1,1),
  28. ItemValue INT,
  29. ItemRank INT
  30. )
  31. AS BEGIN
  32. -- Container for XML
  33. DECLARE
  34. @XmlText NVARCHAR(MAX),
  35. @Xml XML;
  36. -- Remove empty inner items
  37. WHILE (CHARINDEX(@Delim + @Delim, @Array, 0) > 0) BEGIN
  38. SET @Array = REPLACE(@Array, @Delim + @Delim, @Delim);
  39. END;
  40. -- Remove empty left item
  41. IF(LEFT(@Array, 1) = @Delim) BEGIN
  42. SET @Array = SUBSTRING(@Array, 2, LEN(@Array)-1)
  43. END;
  44. -- Remove empty right item
  45. IF(RIGHT(@Array, 1) = @Delim) BEGIN
  46. SET @Array = SUBSTRING(@Array, 1, LEN(@Array)-1)
  47. END;
  48. -- Prepare XML-Text
  49. SET @XmlText = N'<List><Item>' +
  50. REPLACE(@Array, @Delim, N'</Item><Item>') +
  51. N'</Item></List>';
  52. -- Convert Array to XML
  53. SET @Xml = CAST(@XmlText AS XML);
  54. -- Temp Table-Variableble
  55. DECLARE @TempData TABLE(
  56. ItemIndex INT IDENTITY(1,1),
  57. ItemValue NVARCHAR(MAX)
  58. )
  59.  
  60. -- Extract Array Items to temp Table-Variable
  61. INSERT INTO @TempData
  62. SELECT
  63. Item = item.value('.', 'INT')
  64. FROM
  65. @Xml.nodes('//Item') XMLDATA(item)
  66. WHERE
  67. -- Skeep non-numeric items
  68. ISNUMERIC(item.value('.', 'NVARCHAR(MAX)')) = 1;
  69. -- Calculate Rank for each item (to find non-unique items)
  70. INSERT INTO @Data(ItemValue, ItemRank)
  71. SELECT
  72. ItemValue,
  73. ItemRank=RANK() OVER(PARTITION BY ItemValue ORDER BY ItemIndex, ItemValue)
  74. FROM
  75. @TempData
  76. ORDER BY
  77. ItemIndex;
  78.  
  79. RETURN;
  80. END;
  81.  
  82. GO
  83.  
  84. -- Get all non-empty and numeric items
  85. SELECT * FROM dbo.fnArray2IntTable(N';444;bbb;333;;;333;444;555;', N';');
  86. -- Get all non-empty and numeric items and filter non-unique
  87. SELECT * FROM dbo.fnArray2IntTable(N';444;bbb;333;;;333;444;555;', N';') WHERE ItemRank=1;


Result of execution: