Miscellaneous JavaScript code

I’ve been going through a lot of my old(er) code recently, and I found these two files. They define functions for checking what type a variable is, and for determining various qualities of the variable. Not very exciting, I know, but they’re pretty useful.

/* typeOf(x) -- returns the type of the variable x. */
var typeOf = function(x) {
    if(x === null) {
        return "null";
    }
    if(x === undefined) {
        return "undefined";
    }
    if(x !== x) {
        return "NaN";
    }
    var type = typeof x;
    if(type != "object") {
        return type;
    }
    return Object.prototype.toString.call(x).slice(8, -1);
};
/* isDefined(x) -- returns true is x is defined, and false otherwise. */
var isDefined = function(x) {
    return x !== undefined;
};

/* isSomething(x) -- returns true if x is something (and false otherwise), based on the following qualities:

* it must be defined
* it must not be null
* if it's a number then it must not be zero
* if it's a string then it must not be the empty string
* if it's array-like then it must not have a length of zero
* if it's an object then it must have at least one enumerable non-inherited property
*/
var isSomething = function(x) {
    if(!isDefined(x) || x === null) {
        return false;
    }
    if(typeof x == "number" && x === 0) {
        return false;
    }
    if(typeof x == "string" && x === "") {
        return false;
    }
    if(isArrayLike(x) && x.length == 0) {
        return false;
    }
    if(typeof x == "object" && isEmptyObject(x)) {
        return false;
    }
    return true;
};

/* isNothing(x) -- returns the opposite of isSomething(x). */
var isNothing = function(x) {
    return !isSomething(x);
};

/* isEmptyObject(x) -- returns true if x is an object and has no enumerable non-inherited properties, and false otherwise. */
var isEmptyObject = function(x) {
    if(typeof x != "object") {
        return null;
    }
    for (var i in x) {
        if(x.hasOwnProperty(i)) {
            return false;
        }
    }
    return true;
};

/* isArrayLike(x) -- returns true if x is an array or is similar to an array, and false otherwise. */
var isArrayLike = function(x) {
    return x && typeof x == "object" && isDefined(x.length) && isFinite(x.length) && x.length >= 0 && x.length === Math.floor(x.length) && x.length < 4294967296;
};

/* isSameTypeAs(x, y) -- returns true if the type of x is the same as the type of y, and false otherwise. */
var isSameTypeAs = function(x, y) {
    return typeof x === typeof y;
};
Advertisements

Getting a new computer

So, I’ll be getting a new computer in about a week or so. It’s a Toshiba Satellite P855-S5200, and is pretty good.

Specs if anyone is interested:

  • Intel Core i7-3610QM (quad-core, 2.3 GHz with Turbo Boost up to 3.3 GHz, 6 MB L3 cache)
  • Mobile Intel HD graphics, 64 MB – 1696 MB dynamically allocated graphics memory
  • 750 GB 5400 rpm Serial ATA HDD
  • 8 GB DDR3 1600 MHz
  • I’m too lazy to post the rest.

It’s rather better than my current computer, which is an IBM ThinkPad T41. (With an Intel Pentium M 1.6 GHz processor, 1 GB RAM, and 160 GB HDD. Hey, it’s not bad if you live in 2003.)

After I get it it might be a while before a post anything new about my programming stuff, as I’ll have to get it everything installed, and files copied over, and all that.

pytextedit 0.2: pretty menus

Introducing the new and improved Menu™, now with images!

This should make it easier to find what you’re looking for just based on the icon, instead of having to read through the all menu items.

Also, you can see the new status bar in this screenshot as well. I finally got the labels to update correctly. As it turns out, this was just due to a stupid mistake; I updated on <Key> and <Button-1>, while I should have updated on <KeyRelease> and <ButtonRelease-1>.

Changes in pytextedit 0.2, Sept. 19

I’ve been sort of busy:

  • Font size can now no longer be zero or negative. This would cause a lot of inconsistencies.
  • Right-clicking on toolbar buttons now show dialogs with a short description of what the button does. Sometime in the future I’ll improve this further by using little help balloon things (I have no idea what they’re called) instead of a showinfo().
  • Middle-clicking now pastes text.
  • Fixed bug in “Goto” where clicking Cancel in the dialog would see() the line with the cursor. (This was a problem if the user had scrolled.)
  • “Goto” now uses the current line number as the default value.
  • “Select From” now uses the current cursor position as the default value.
  • Removed some unnecessary global variables.
  • askdirectory() is now imported in Python 2 as well. I have no idea why it wasn’t before.
  • askopenfilename() is now imported. It was used by “Edit Favorites”, but it wasn’t imported. Oops. I really need to stop making stupid mistakes like this…
  • “Edit Favorites” now uses ED_INIT_DIR to set the default directory.
  • “Edit Favorites” now uses the custom filetypes list.
  • Changed keybinding for “Favorites” to Ctrl+Alt+[Space]. (Ctrl+Tab will be used by the MDI later.)
  • “Recently Opened” can now be used easily without a mouse. (Added “Return” keybinding, and focus()ed on the listbox.)
  • “Favorites” can now be used easily without a mouse. (Added “Return” keybinding, and focus()ed on the listbox.)
  • “View Bookmarks” can now be used easily without a mouse. (Added “Return” keybinding, and focus()ed on the listbox.)
  • Default value for “Quick Open” is now ED_INIT_DIR.
  • Removed “Indent All” and “Unindent All”. See next improvement.
  • “Indent” and “Unindent” will now indent any selected text. This makes “Indent All” and “Unindent All” irrelevant, as all you have to do now is select all the text.
  • Added keybinding for “Add Bookmark.” (Ctrl+B) This makes it much quicker to use.
  • Added “Documents” menu. This will be used for the MDI later.
  • Added “Previous” and “Next” toolbar buttons. Again, these will be used for the MDI.
  • Re-organized toolbar. It just looked a little strange.
  • Bookmarks now cannot be added multiple times on the same line.
  • Added “Search -> Open URL in Web Browser”. Very useful for README files, in my opinion.
  • Added “Options -> Enlarge Font” and “Options -> Shrink Font”. There were no menu options for them before. Which was a bit of a problem, because without that there was no way to know the feature was there.
  • Enabling/disabling tearoff menus in Options dialog no longer requires a restart.
  • Fixed bug where tearoff menu OptionMenu in Options dialog would not default to disabled. The values didn’t match in the conditional in the if block.
  • Bookmarks are now sorted numerically. This makes it much easier to quickly find the bookmark you want.
  • Added keybinding for “View Bookmarks” (Ctrl+Shift+B).
  • Removed menu tearoff for bookmarks submenu in context menu. I have no clue why that was there, as it was completely unnecessary. Probably just another time where I was copy-pasting code and not paying much attention…
  • Revised “About” dialog.
  • Fixed bug where “Goto” would accept a line greater than the total number of lines. It seems I mass-replaced a certain string, and it matched a line in this function that I didn’t intend it to. Once again, oops.
  • Bookmarks will now no longer be shown if the line number is greater than the total number of lines. Before it would cause an error if you tried to go a bookmark on a line that didn’t exist.
  • In “Statistics” dialog added “Chars (excluding spaces)” and “Sel chars (excluding spaces)”.
  • “Statistics” dialog will now only show info on selected text if there is text selected.
  • Added feature to add extra indenting based on last character of the line. This allows for easier writing of code. Basically, it’s meant for auto-indenting blocks of code. So, if the option is set to “python”, it will add extra indent after a line that ends with “:”, and if the language is set to “c”, it will add extra indent after a line that ends with “{“. This can be turned off as well, of course.
  • Added a Cancel button to the “Options” dialog.
  • “Insert Date/Time” now use standard formatting.
  • “Undo” and “Redo” will no longer cause errors if there is nothing to do. Yay for catchall try-except blocks.
  • Moved time/date formatting options into “Misc” tab in “Options” dialog.
  • Added block indenting option to “Editing” tab in “Options” dialog.

Changes in pytextedit 0.2

Even though I just finished pytextedit 0.1 a little bit ago, I’ve already started working on version 0.2.

Probably the biggest addition to this version is a multi-document interface (MDI). This will allow the user to be editing more than one file at a time, without needing to have multiple windows open. In terms of the interface there aren’t many changes that are obvious to the user; the only additions are two toolbar buttons, a Documents menu, and some keyboard shortcuts. The code, however, isn’t anywhere near that simple.

Aside from the MDI, though, there aren’t going to be many new features. Instead, I’m going to focus on revising and improving the existing ones. For example, “Open From URL” will get Unicode support, bookmarks will have a lot of minor improvements to make them more useful, and various dialogs will be easier to use with only the keyboard.

I’ve also been experimenting with syntax highlighting, but I don’t know if it will go into version 0.2, considering what I currently have doesn’t work very well.

Also, the status bar might be back, if I can get the statistics labels to work correctly. The old ones wouldn’t always display the correct position/statistic, which as you can probably imagine led to them being absolutely useless. Unless, of course, you enjoy watching random numbers get bigger and smaller.

extmath.js: JavaScript Math object extension

This is a fairly simple and small JavaScript library that extends the native JavaScript Math object. I created this really just because I was working on a lot of math-related projects with JavaScript so it made sense to collect everything I used into one file. I actually wrote most of this about a year ago, forgot about it, then found it again recently and updated it.

Here’s a list of all the new functions it defines:

  • Math.add(x) – Returns the sum of an array of numbers. This might seem kind of useless considering you can just use +, but when you have an array of numbers this is actually quite handy.
  • Math.subtract(x) – Returns the difference of an array of numbers.
  • Math.multiply(x) – Returns the product of an array of numbers.
  • Math.divide(x) – Returns the quotient of an array of numbers.
  • Math.modulus(x) – Returns the modulus of an array of numbers.
  • Math.factorial(x) – Returns the factorial of a number.
  • Math.range(x) – Returns the range of an array of numbers.
  • Math.mean(x) – Returns the mean (average) of an array of numbers.
  • Math.isInRange(x, y, z) – Returns true if x is between y and z, inclusive, and false otherwise.
  • Math.isInRange2(x, y, z) – Returns true if x is between y and z, exclusive, and false otherwise.
  • Math.acosec(x) – Returns the inverse cosecant of a number.
  • Math.acosech(x) – Returns the inverse hyperbolic cosecant of a number.
  • Math.acosh(x) – Returns the inverse hyperbolic cosine of a number.
  • Math.acot(x) – Returns the inverse cotangent of a number.
  • Math.acoth(x) – Returns the hyperbolic inverse cotangent of a number.
  • Math.asec(x) – Returns the inverse secant of a number.
  • Math.asech(x) – Returns the inverse hyperbolic secant of a number.
  • Math.asinh(x) – Returns the inverse hyperbolic sine of a number.
  • Math.atanh(x) – Returns the inverse hyperbolic tangent of a number.
  • Math.cosec(x) – Returns the cosecant of a number.
  • Math.cosech(x) – Returns the hyperbolic cosecant of a number.
  • Math.cosh(x) – Returns the hyperbolic cosine of a number.
  • Math.cot(x) – Returns the cotangent of a number.
  • Math.coth(x) – Returns the hyperbolic cotangent of a number.
  • Math.sec(x) – Returns the secant of a number.
  • Math.sech(x) – Returns the hyperbolic secant of a number.
  • Math.sign(x) – Returns 0 if the number is 0, -1 is the number is negative, and 1 if the number is positive.
  • Math.sinh(x) – Returns the hyperbolic sine of a number.
  • Math.tanh(x) – Returns the hyperbolic tangent of a number.
  • Math.crt(x) – Returns the cube root of a number.
  • Math.rt(x, y) – Returns the y root of x.
  • Math.isEven(x) – Returns true if a number is even, and false otherwise.
  • Math.isPrime(x) – Returns true if a number is prime, and false otherwise.
  • Math.mode(x) – Returns the most often occurring number in an array.
  • Math.median(x) – Returns the median of an array of numbers.
  • Math.random([x [, y]]) – If it is called without any arguments, this returns a pseudo-random number between 0 and 1. If it is called with one argument, this returns a pseudo-random number between 0 and x. If it is called with two arguments, this returns a pseudo-random number between x and y.
  • Math.isInteger(x) – Returns true if a number is an integer, and false otherwise.
  • Math.isNatural(x) – Returns true if a number is natural, and false otherwise.
  • Math.stdDev(x) – Returns the standard deviation of an array of numbers.
  • Math.quadEquation(a, b, c) – Returns the results of the quadratic equation using a, b, and c. If there are no real answers, it will return false.

It also defines a Math.Complex() class for complex numbers:

  • Math.Complex(real, imaginary)– This is the constructor.
  • complex.add(complex2) – Returns a new Math.Complex object that is the sum of complex and complex2.
  • complex.subtract(complex2) – Returns a new Math.Complex object that is the difference of complex and complex2.
  • complex.multiply(complex2) – Returns a new Math.Complex object that is the product of complex and complex2. Note that complex2 can be either a Math.Complexobject or a number.
  • complex.divide(complex2) – Returns a new Math.Complex object that is the quotient of complex and complex2. Note that complex2 can be either a Math.Complex object or a number.
  • complex.square() – Returns a new Math.Complex object that is complex squared.
  • complex.magnitude() – Returns the magnitude of complex.
  • complex.conjugate() – Returns a new Math.Complex object that is the conjugate of complex.
  • complex.negative() – Returns a new Math.Complex object that is the negative of complex.
  • complex.equals(complex2) – Returns true if complex and complex2 and equal, and false otherwise.
  • complex.valueOf() – This is the same as complex.magnitude(). It is called whenever complex needs to be automatically converted to a number.
  • complex.toString() – Returns complex in string form. It is called whenever complex needs to be automatically converted to a string.

If for some reason you’re interested in this, you can download it here.