Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

String core exts.

  • Loading branch information...
commit b207160913aab40abe577b46af57972ed3972ed1 1 parent 17c99cb
@vesln authored
Showing with 117 additions and 0 deletions.
  1. +12 −0 Makefile
  2. +6 −0 lib/index.js
  3. +59 −0 lib/string.js
  4. +40 −0 test/string.test.js
View
12 Makefile
@@ -0,0 +1,12 @@
+TESTS = test/*.test.js
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --require should \
+ --reporter spec \
+ $(TESTS)
+
+init:
+ chmod 777 ./data
+
+.PHONY: test init
View
6 lib/index.js
@@ -0,0 +1,6 @@
+/*!
+ * core_ext - JavaScript extensions.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
View
59 lib/string.js
@@ -0,0 +1,59 @@
+/*!
+ * core_ext - JavaScript extensions.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Returns char at specified position.
+ *
+ * @param {Number} Index.
+ * @returns {String} Char.
+ * @api public
+ */
+String.prototype.at = function(i) {
+ return this[i];
+};
+
+/**
+ * Returns the first char of a string.
+ *
+ * @returns {String} Char.
+ * @api public
+ */
+String.prototype.first = function() {
+ return this[0];
+};
+
+/**
+ * Returns the last char of a string.
+ *
+ * @returns {String} Char.
+ * @api public
+ */
+String.prototype.last = function() {
+ return this[this.length - 1];
+};
+
+/**
+ * Returns the last char of a string.
+ *
+ * @param {Number} Index.
+ * @returns {String} Char.
+ * @api public
+ */
+String.prototype.from = function(i) {
+ return this.substring(i, this.length);
+};
+
+/**
+ * Returns the last char of a string.
+ *
+ * @param {Number} Index.
+ * @returns {String} Char.
+ * @api public
+ */
+String.prototype.to = function(i) {
+ return this.substring(0, this.length - i - 1);
+};
View
40 test/string.test.js
@@ -0,0 +1,40 @@
+/*!
+ * core_ext - JavaScript extensions.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+var string = require('../lib/string');
+
+describe('String', function() {
+ describe('.at()', function() {
+ it('should have .at method that returns char from specified position', function() {
+ 'foo'.at(1).should.eql('o')
+ });
+ });
+
+ describe('.first()', function() {
+ it('should return the first char of a string', function() {
+ 'foo'.first().should.eql('f')
+ });
+ });
+
+ describe('.last()', function() {
+ it('should return the first char of a string', function() {
+ 'foof'.last().should.eql('f');
+ });
+ });
+
+ describe('.from()', function() {
+ it('should return the part of a string from supplied index', function() {
+ 'foof'.from(1).should.eql('oof');
+ });
+ });
+
+ describe('.to()', function() {
+ it('should return the part of a string to supplied index', function() {
+ 'foof'.to(1).should.eql('fo');
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.