Loving Ubiquity; Extending the Web in 2009 Frustrating User Experiences: Remember what I say Wordpress!
Jan 07

TokenObject: A strange little piece of code that creates objects for me

JavaScript Add comments

Do you ever write random bits of code and wonder why you are doing it? Something that seems worth it at the time and then you really wonder?

I just did this with a class that I call TokenObject. The goal of this trivial class is to take a string, and give me a nice way to get pieces out of it. Normally I would use split and grab the right piece, but I was doing so much conversion from “command line string” to an object, that I ended up with something that lets me do this:

var userpass = "dion password";
var up = new TokenObject(userpass, {params: "user pass"});
// up.user;
// up.pass;
// Can also get via: up.param('user'); up.param(0);

Funny huh? :)

/*
 * TokenObject: Given a string, make a token object that holds positions and has name access
 */
 
var TokenObject = function(input, options) {
	this._input = input;
	this._options = options;
	this._splitterRegex = new RegExp(this._options.splitBy || '\\s+');
	this._pieces = input.split(this._splitterRegex);
 
	if (this._options.params) { // -- create a hash for name based access
		this._nametoindex = {};
		var namedparams = this._options.params.split(' ');
		for (var x = 0; x < namedparams.length; x++) {
			this._nametoindex[namedparams[x]] = x;
 
			if (!this._options['noshortcutvalues']) { // side step if you really don't want this
				this[namedparams[x]] = this._pieces[x];
			}
		}
 
	}
}
 
TokenObject.prototype.param = function(index) {
	return (typeof index == "number") ? this._pieces[index] : this._pieces[this._nametoindex[index]];
}
 
TokenObject.prototype.length = function() {
	return this._pieces.length;
}

2 Responses to “TokenObject: A strange little piece of code that creates objects for me”

  1. SD Says:

    Check these two vbscript versions of extracting a string or part of it from a larger string. e.g from a string “Monday, January, 08, 2009″ it extracts “Monday, Jan, 08,2009″.

    a=(FormatDateTime(Date(),1))
    length=len(a)

    pos=instr (a, “,”)
    l=pos+4

    one=left(a,l)

    pos1=instrrev(a, “,”)
    l1=(length-pos1)+3
    two=right(a,l1)

    msgbox one & ” ” & two

    Another version:

    dim d(0), arr(40), farr(26), fa(26)

    a=(FormatDateTime(Date(),1))

    length=len(a)
    p=length

    ‘PUTTING STRING a INTO ARRAY arr
    for i=1 to length
    t=left(a,1)
    a=right(a,length-i)
    arr(i)=t
    next

    ‘EXTRACTING UPTILL 4 CHARACTERS AFTER FIRST COMA FOR EXAMPLE Thursday, Jan
    for i=1 to length
    if arr(i) = “,” then
    length = i+4
    exit for
    end if
    next

    for i=1 to length
    fa(i)=arr(i)
    next

    ‘NOW f1 CONTAINS FOR EXAMPLE Thursday, Jan
    f1=join(fa)

    ‘EXTRACTING LAST 8 CHARACTERS FROM ARRAY arr FOR EXAMPLE 08, 2009
    for e=1 to 8
    farr(e)=arr(p)
    p=p-1
    next

    xx=join (farr)
    final_str=trim(f1) & “, ” & trim(strreverse(xx))
    msgbox final_str

  2. steve Says:

    Uhm, I don’t get it… whats the purpose here?

    If you want quick easy access to bits of info.. stuff them in an Object… it works like a Map… and can store strings, ints, functions and more.

    var data = {};
    data.user = ‘David’;
    data.pass = ’secret’;
    data.age = 37;
    data.spillTheBeans = function(){return ‘User:’ + this.user + ‘ Pass:’ + this.pass + ‘ Age:’ + this.age};

    If you just want to simplify the “Map” to have get/set methods you can wrap it up into a simple class…

    function MapThing(){
    this.data = {};
    this.get = function(key){
    return this.data[key];
    };
    this.set = function(key, value){
    this.data[key] = value;
    };
    this.length = function(){
    var count = 0;
    for(var x in this.data){
    count++;
    }
    return count;
    };
    }

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'cricket'