chuynh18 / JSMethods

reimplementing built-in JavaScript methods for fun and profit (and learning)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

reimplementing built-in JS methods

tl;dr

I'm reimplementing some of JavaScript's built in methods for fun. Since these are functions, they won't be invoked with the same exact syntax, but their functionality should be very comparable.

Execution

These .js files can be run inside Node.js. (No reason they shouldn't work in a browser context if you embed them into an HTML file and check out the console.)

Caveats

Note that not all functionality is being reimplemented. For example, my charCodeAt only recognizes some of the Basic Latin characters in the Unicode standard. This is a tiny, tiny subset of Unicode. My goal was more to make sure I understand the logic behind each method; including the entire Unicode standard would just mean searching through a larger dictionary.

String methods

* denotes partial implementation
charAt - returns the character at the specified index
charCodeAt* - returns the Unicode value of the character at the specified index in a string. Inverse of fromCharCode.
concat - joins two or more strings
endsWith - returns whether string ending matches input character(s)
fromCharCode* - returns the character corresponding to a given Unicode value. Inverse of charCodeAt.
includes - returns whether a string contains a specified substring
indexOf - returns the position inside a string of the first occurence of a specified substring. Returns -1 if the string doesn't contain the specified substring.
lastIndexOf - returns the position inside a string of the last occurence of a specified substring. Returns -1 if the string doesn't contain the specified substring.
repeat - returns a string that consists of a specified number of copies of the input string
replace* - searches a string for a specified value and returns a new string where the specified values have been replaced with an input string. No support for regular expressions.
slice - returns the selected part of the input string as a new string
split - returns an array of substrings from a given string
startsWith - returns whether string beginning matches input character(s)
substr - extracts a substring from a string and returns it
substring - extracts a substring between two specified indices and returns it
toLowerCase* - converts a string to lowercase letters. Only handling letters of the English alphabet.
toUpperCase* - converts a string to uppercase letters. Only handling letters of the English alphabet.
trim - given an input string, removes leading and trailing whitespaces, then returns a new string

String methods I am not implementing

localeCompare - skipping for now
match - skipping for now... I may tackle this at some point in the future. I've been reading up on Thompson's construction and automata theory, then I might try writing a basic regex parser, though I doubt it will be complete.
search - like indexOf, except it supports regex. However, I am not writing a regex parser yet.
toLocaleLowerCase - skipping, see toLowerCase
toLocaleUpperCase - skipping, see toUpperCase
toString - don't think this adds any value in terms of learning
valueOf - don't think this adds any value in terms of learning

About

reimplementing built-in JavaScript methods for fun and profit (and learning)


Languages

Language:JavaScript 100.0%