Category Archives: software - Page 2

Code of the day: Converts Bytes to Simplify Units

A few days ago I noticed that my “code of the day” article was wrong. So I spent some time fixing it and provided test cases to check my work.
The main difference about this script is that it allows you to support both the SI and IEC standard and fixes a few rounding errors.

// function: getBytesWithUnit
// input: bytes (number)
// input: useSI (boolean), if true then uses SI standard (1KB = 1000bytes), otherwise uses IEC (1KiB = 1024 bytes)
// input: precision (number), sets the maximum length of decimal places.
// input: useSISuffix (boolean), if true forces the suffix to be in SI standard. Useful if you want 1KB = 1024 bytes
// returns (string), represents bytes is the most simplified form.
var getBytesWithUnit = function (bytes, useSI, precision, useSISuffix) {
	"use strict";
	if (!(!isNaN(bytes) && +bytes > -1 && isFinite(bytes))) {
		return false;
	}
	var units, obj,	amountOfUnits, unitSelected, suffix;
	units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	obj = {
		base : useSI ? 10 : 2,
		unitDegreeDiff : useSI ? 3 : 10
	};
	amountOfUnits = Math.max(0, Math.floor(Math.round(Math.log(+bytes) / Math.log(obj.base) * 1e6) / 1e6));
	unitSelected = Math.floor(amountOfUnits / obj.unitDegreeDiff);
	unitSelected = units.length > unitSelected ? unitSelected : units.length - 1;
	suffix = (useSI || useSISuffix) ? units[unitSelected] : units[unitSelected].replace('B', 'iB');
	bytes = +bytes / Math.pow(obj.base, obj.unitDegreeDiff * unitSelected);
	precision = precision || 3;
	if (bytes.toString().length > bytes.toFixed(precision).toString().length) {
		bytes = bytes.toFixed(precision);
	}
	return bytes + " " + suffix;
};




Demo: Test Script here


Want to learn more about Javascript?
Check out this “Professional JavaScript for Web Developers”.

Try out lumzy for your mockup creation and prototyping needs. 100% free



I haven’t been paid to say this. I just want others to know about Lumzy instead of paying for similar mockup software that does the same thing.

Siebel eScript headaches.


Here are a few problems that I’ve encountered while spending a week programming in Siebel eScript 7.8.

  1. Siebel eScript is ECMAScript compliant.
    ECMAScript is the standard implementation of JavaScript as defined by the ECMA-262 standard.
    Beginning with Siebel Business Applications version 7.8, the script engine supports ECMAScript Edition 4.
    You can opt to use the updated functionality of this engine, including the Siebel ScriptAssist tool, or you can opt to leave this updated functionality inactive.
    http://docs.oracle.com/cd/B31104_02/books/eScript/eScript_JSLOverview.html

    Here’s a link to ECMAScript Version 4.0. proposed draft.
    Example code for ES4.

    interface I { function f() } 
    interface J { function g(n) } 
    class C implements I, J { 
    	function f() { return "foo" } 
    	function g(n) { return n+2 } 
    }


    It should be noted that most major web browsers abandoned ES4 due it the complexity of feature implementations.
    So even if your scripts pass jshint.com and jslint.com 100%, and works on all major browsers you still have the possibility of your code throwing an during execution.
    Siebel eScript says it’s ECMA-262 compliant but I’ve found a few bugs in the engine that require some work around.

  2. Developing a simple way to unit test your code is essential.
    Here’s a easy way to add test function to your library.

    function logThis( name, result ){
    	// Log this values to a Property Set Object that get's converted to XML.
    	AddParameterPS( logObjs.Outputs, name, result, "logThis");
    }
    function testThis( name, func ){
    	try{
    		logThis( "Passed: "+ name, func() );
    	}
    	catch(e){
    		logThis( "Failed: "+ name, e );
    	}
    }


    Test example

    function runTest(Input, Output){
     
    	testThis( "Test strict equivalence support", function(){
    		return "1" !== 1;
    	});
    	// This will fail since Siebel doesn't support the apply and call Object function calls.
    	testThis( "Test isArray using call", function(){
    		// Siebel eScript: FAIL, Browser: Pass
    		function isArray( arr ){
    			return Object.prototype.toString.call( obj ) == "[Object Array]";
    		}
    	});
    	testThis( "Test isArray using instanceof", function(){
    		// Siebel eScript: Pass, Browser: Pass
    		function isArray( arr ){
    			return arr instanceof Array;
    		}
    	});
    }

  3. Siebel Business Applications in high-interactivity mode requires an ActiveX plugin, which forces you to use Internet Explorer on Windows.
  4. Siebel eScript has errors with it’s engine and it’s hard to debug eScripts for run-time business services.
    Here’s a strange bug in Siebel eScript.
    You can’t create return objects when they are generated from a function attached to an object.
    Example Code:

    	var x = {};
    	x.getObj = function( a, b ){
    		var obj = function(){
    			this.x = a;
    			this.y = b;
    		};
    		return new obj();
    	};
    	var getObj = function( a, b ){
    		var obj = function(){
    			this.x = a;
    			this.y = b;
    		};
    		return new obj();
    	};
    	var arr = [];
    	arr.push( getObj() );	// successful
    	arr.push( x.getObj() );	// Run-time Error.

  5. Siebel eScript engine is pretty damn slow. So avoid writing these two performance killers.
    1: Wrapping all your code within a closure.
    2: Defining an object inside closure with the same name as an object in the outer scope.
    Example:

    	var x = (function(){
    		var x = {
    			a:1
    		};
    		return x;
    	}());

Siebel eScript Reference from Oracle.com:
eScript Lanague Reference (2004 edition)

Simple fix for MSI Windows Installer Error

Here’s a quick post over how to install applications that encounter the Windows Installer Error.


Error Message:

Windows Installer
This installation package could not be opened.
Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.

Alternative Solutions not presented in the video.
1) Avoid msi files and go for an alternative file format to install your application.
2) Install the application using the hidden Administrator account.
Here’s a Guide
3) Reinstall Windows on your computer.
4) Re-download the file from a different website.
5) System Restore to a period where the problem didn’t exist.
6) Extract the content of the MSI file and run the executable from there.
Extract MSI contents.batch

REM Extracts the filename.msi file to C:\extracted_files
msiexec /a filename.msi /qb TARGETDIR=C:\extracted_files

Bad Behavior has blocked 1086 access attempts in the last 7 days.

FireStats icon Powered by FireStats