Skip to content

Commit

Permalink
Merge branch 'master' into packageupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcharlie committed Mar 29, 2018
2 parents 717bb73 + 2e4d6ff commit faeb3b9
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 98 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ var myArray = [1, 2, 3, 4, 5];
console.log(myArray.last()); // 5
```

#### Array.frontPad(item, length)

This function will return a new array with padding to the length specified. If length specified is less than array length the same array will be returned.

```
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.frontPad("item", 8)); // ["item", "item", "item", 1, 2, 3, 4, 5]
```

```
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.frontPad("item", 3)); // [1, 2, 3, 4, 5]
```

#### Array.backPad(item, length)

This function will return a new array with padding to the length specified. If length specified is less than array length the same array will be returned.

```
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.backPad("item", 8)); // [1, 2, 3, 4, 5, "item", "item", "item"]
```

```
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.backPad("item", 3)); // [1, 2, 3, 4, 5]
```


### Numbers

Expand Down Expand Up @@ -306,3 +334,21 @@ Promise.reflect([myPromise, myPromiseB, myPromiseC]).then(function (state) {
console.log(state); // [{v:"OK", status:"fulfilled"}, {e:"Fail", status:"rejected"}, {v:"OK", status:"fulfilled"}]
});
```

### Timeout

#### scriptutils.timeout(ms)

This function will return a promise that will resolve after the number of milliseconds passed in. The promise will reject if an invalid number of milliseconds are passed in. **This function is only available in the Node.js version of scriptutils**.

```
scriptutils.timeout(1000).then(function() {
console.log("This will be run after 1 second");
});
```

```
scriptutils.timeout("test").catch(function() {
console.log("This will be run because the number of milliseconds is invalid");
});
```
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gulp.task('browser', function () {
return browserify({entries: './index.js', extensions: ['.js'], debug: true, basedir: __dirname + '/src', standalone: 'scriptutils'})
.ignore('./src/hash/index.js')
.ignore('./src/promise/index.js')
.ignore('./src/other/timeout.js')
.transform(babelify)
.bundle()
.pipe(source('index.js'))
Expand Down
60 changes: 60 additions & 0 deletions src/array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,63 @@ Array.prototype.first = function() {
Array.prototype.last = function() {
return this[this.length - 1];
};

/* istanbul ignore if */
if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', {
value: function(value) {

// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}

var O = Object(this);

// Steps 3-5.
var len = O.length >>> 0;

// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;

// Step 8.
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);

// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;

// Step 11.
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);

// Step 12.
while (k < final) {
O[k] = value;
k++;
}

// Step 13.
return O;
}
});
}

Array.prototype.frontPad = function (item, length) {
if (isNaN(length)) {
return this;
}
return [...Array((length - this.length) > this.length ? (length - this.length) : 0).fill(item), ...this];
};

Array.prototype.backPad = function (item, length) {
if (isNaN(length)) {
return this;
}
return [...this, ...Array((length - this.length) > this.length ? (length - this.length) : 0).fill(item)];
};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ require('./string/index.js');
require('./promise/index.js');

var otherNumber = require('./other/number.js');
var timeout = require('./other/timeout.js');
var hash = require('./hash/index.js');
module.exports = {...otherNumber, ...hash};
module.exports = {...otherNumber, ...hash, ...timeout};
9 changes: 9 additions & 0 deletions src/other/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports.timeout = function(ms) {
ms = parseInt(ms);
return new Promise((resolve, reject) => {
if (isNaN(ms)) {
reject(`Invalid miliseconds passed in: ${ms}`);
}
setTimeout(() => resolve(), ms);
});
}
42 changes: 42 additions & 0 deletions test/01_array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,46 @@ describe('Array', function() {
expect(array.last()).to.deep.equal("World");
});
}); // end "Last" describe
describe('Padding', function () {
var array = [];
beforeEach(function () {
array = ["Hello", "World"];
});

describe('Front Padding', function () {
it('Should return accurate array', function () {
expect(array.frontPad("item", 5)).to.deep.equal(["item", "item", "item", "Hello", "World"]);
});
it('Shouldn\'t fail if current array length is greater than length passed in', function () {
expect(array.frontPad("item", 1)).to.deep.equal(["Hello", "World"]);
});
it('Shouldn\'t fail if current array length is equal to array length passed in', function () {
expect(array.frontPad("item", 2)).to.deep.equal(["Hello", "World"]);
});
it('Shouldn\'t fail if nothing is passed into function', function () {
expect(array.frontPad()).to.deep.equal(["Hello", "World"]);
});
it('Shouldn\'t fail if only item passed into function', function () {
expect(array.frontPad("item")).to.deep.equal(["Hello", "World"]);
});
});

describe('Back Padding', function () {
it('Should return accurate array', function () {
expect(array.backPad("item", 5)).to.deep.equal(["Hello", "World", "item", "item", "item"]);
});
it('Shouldn\'t fail if current array length is greater than length passed in', function () {
expect(array.backPad("item", 1)).to.deep.equal(["Hello", "World"]);
});
it('Shouldn\'t fail if current array length is equal to array length passed in', function () {
expect(array.backPad("item", 2)).to.deep.equal(["Hello", "World"]);
});
it('Shouldn\'t fail if nothing is passed into function', function () {
expect(array.backPad()).to.deep.equal(["Hello", "World"]);
});
it('Shouldn\'t fail if only item passed into function', function () {
expect(array.backPad("item")).to.deep.equal(["Hello", "World"]);
});
});
});
}); // end "Array" describe

0 comments on commit faeb3b9

Please sign in to comment.