From e3902e9add84e56147b30edf7f067cf6d880d48f Mon Sep 17 00:00:00 2001 From: Bruno Casanova Date: Wed, 24 Sep 2014 18:19:50 +0100 Subject: [PATCH] Add `Util.Array.empty()` --- README.md | 1 + lib/type/array.js | 11 +++++++++++ test/type/array.test.js | 13 ++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 569994e..91b00b1 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ var Util = require('findhit-util'); Util.Array.each() <- Util.Array.forEach() Util.Array.map() + Util.Array.empty() // String utils // Util.string OR Util.String diff --git a/lib/type/array.js b/lib/type/array.js index f3f73c5..068acd2 100644 --- a/lib/type/array.js +++ b/lib/type/array.js @@ -89,4 +89,15 @@ module.exports = (function ( Util ){ return newArray; }; + $.empty = function ( array ) { + + if ( Util.isnt.Array( array ) ) { + throw new TypeError("array must be an array"); + } + + array.splice( 0, array.length ); + + return array; + }; + }); \ No newline at end of file diff --git a/test/type/array.test.js b/test/type/array.test.js index e4a42b0..ee805ef 100644 --- a/test/type/array.test.js +++ b/test/type/array.test.js @@ -10,7 +10,7 @@ describe( "Util", function () { describe( "array", function () { - describe( "map", function () { + describe( ".map", function () { it( "get 3rd value of each array on array", function () { var t, a = [ @@ -28,6 +28,17 @@ describe( "Util", function () { }); + }); + + describe( ".empty", function () { + + it( "should empty the array", function () { + var array = [ 1, 'foo', 'bar', 'lala', 2 ]; + + Util.Array.empty( array ); + + expect( array ).to.have.length( 0 ); + }); });