Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit 60aad2d

Browse files
committed
Converted code to ES6.
Removed Grunt.
1 parent 2e4bbc8 commit 60aad2d

10 files changed

+140
-217
lines changed

.eslintrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": [
3+
"@neogeek/eslint-config-standards"
4+
],
5+
"rules": {
6+
"consistent-this": 0,
7+
"func-names": 0,
8+
"no-extend-native": 0,
9+
"no-invalid-this": 0,
10+
"prefer-reflect": 0
11+
}
12+
}

.npmignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
tests/
1+
test/
22

33
.travis.yml

Gruntfile.js

-67
This file was deleted.

iknowishouldnt.js

+45-60
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,76 @@
22
* I Know I Shouldn't
33
* https://github.com/neogeek/iknowishouldnt.js
44
*
5-
* Copyright (c) 2014 Scott Doxey
5+
* Copyright (c) 2016 Scott Doxey
66
* Released under the MIT license
77
*/
88

9-
(function () {
10-
11-
'use strict';
12-
13-
/**
14-
* Returns a formated string.
15-
*
16-
* @example 'Hello %s'.format('World!'); // Hello World!
17-
* @param {String|Number} [arguments] Multiple arguments can be passed as either strings or numbers.
18-
* @return {String} Formatted string.
19-
* @public
20-
*/
21-
22-
function format() {
23-
24-
var value = this,
25-
matches = value.match(/(%[sidf])/g),
26-
replacements = Array.prototype.slice.call(arguments);
27-
28-
if (matches) {
9+
/**
10+
* Returns a formated string.
11+
*
12+
* @example 'Hello %s'.format('World!'); // Hello World!
13+
* @param {String|Number} [arguments] Multiple arguments can be passed as either strings or numbers.
14+
* @return {String} Formatted string.
15+
* @public
16+
*/
2917

30-
matches.forEach(function (match, key) {
18+
const format = function (...replacements) {
3119

32-
if (replacements[key] !== undefined) {
20+
let value = this;
3321

34-
if (match === '%i' || match === '%d') {
22+
(value.match(/(%[sidf])/g) || []).forEach((match, key) => {
3523

36-
replacements[key] = parseInt(replacements[key], 10);
24+
if (typeof replacements[key] !== 'undefined') {
3725

38-
} else if (match === '%f') {
26+
if (match === '%i' || match === '%d') {
3927

40-
replacements[key] = parseFloat(replacements[key]);
28+
replacements[key] = parseInt(replacements[key], 10);
4129

42-
}
30+
} else if (match === '%f') {
4331

44-
value = value.replace(match, replacements[key]);
32+
replacements[key] = parseFloat(replacements[key]);
4533

46-
}
34+
}
4735

48-
});
36+
value = value.replace(match, replacements[key]);
4937

5038
}
5139

52-
return value;
53-
54-
}
55-
56-
Object.defineProperty(String.prototype, 'format', {
57-
58-
enumerable: false,
59-
60-
value: format
61-
6240
});
6341

64-
/**
65-
* Splits an array into separate chunks based on the length specified.
66-
*
67-
* @example [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].chunk(2); // [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9] ]
68-
* @param {Number} num The max size of each chunk.
69-
* @return {Array} Multidimensional array containing the separated chunks.
70-
* @public
71-
*/
42+
return value;
7243

73-
function chunk(num) {
44+
};
7445

75-
var array = [];
46+
Object.defineProperty(String.prototype, 'format', {
47+
'enumerable': false,
48+
'value': format
49+
});
7650

77-
while (this.length) {
51+
/**
52+
* Splits an array into separate chunks based on the length specified.
53+
*
54+
* @example [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].chunk(2); // [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9] ]
55+
* @param {Number} num The max size of each chunk.
56+
* @return {Array} Multidimensional array containing the separated chunks.
57+
* @public
58+
*/
7859

79-
array.push(this.splice(0, num));
60+
const chunk = function (num) {
8061

81-
}
62+
const array = [];
63+
64+
while (this.length) {
8265

83-
return array;
66+
array.push(this.splice(0, num));
8467

8568
}
8669

87-
Object.defineProperty(Array.prototype, 'chunk', {
88-
enumerable: false,
89-
value: chunk
90-
});
70+
return array;
71+
72+
};
9173

92-
}());
74+
Object.defineProperty(Array.prototype, 'chunk', {
75+
'enumerable': false,
76+
'value': chunk
77+
});

iknowishouldnt.min.js

-8
This file was deleted.

package.json

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
{
2-
"name": "iknowishouldnt.js",
3-
"description": "A micro-library that contains snippets of code useful for modifying strings and arrays.",
4-
"version": "0.0.4",
5-
"main": "iknowishouldnt.js",
6-
"devDependencies": {
7-
"grunt": "0.4.5",
8-
"grunt-contrib-watch": "0.6.1",
9-
"grunt-contrib-jasmine": "0.8.1",
10-
"grunt-contrib-uglify": "0.7.0",
11-
"grunt-jslint": "1.1.12",
12-
"grunt-notify": "0.4.1",
13-
"matchdep": "0.3.0"
14-
},
15-
"scripts": {
16-
"test": "grunt test"
17-
},
18-
"private": true
2+
"name": "iknowishouldnt.js",
3+
"description": "A micro-library that contains snippets of code useful for modifying strings and arrays.",
4+
"version": "0.0.5",
5+
"main": "iknowishouldnt.js",
6+
"devDependencies": {
7+
"@neogeek/eslint-config-standards": "1.6.2",
8+
"eslint": "3.7.1",
9+
"mocha": "3.1.0"
10+
},
11+
"scripts": {
12+
"test": "eslint iknowishouldnt.js test/ && mocha test/**/*.js"
13+
},
14+
"private": true
1915
}

test/.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"@neogeek/eslint-config-standards/.eslintrc-tests"
4+
]
5+
}

test/specs/array.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const assert = require('assert');
2+
3+
require('../../iknowishouldnt.js');
4+
5+
describe('Array.prototype.chunk', () => {
6+
7+
it('Testing array with 10 values.', () => {
8+
9+
assert.deepEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].chunk(2), [
10+
[0, 1],
11+
[2, 3],
12+
[4, 5],
13+
[6, 7],
14+
[8, 9],
15+
[10]
16+
]);
17+
18+
});
19+
20+
it('Testing empty array.', () => {
21+
22+
assert.deepEqual([].chunk(2), []);
23+
24+
});
25+
26+
});

test/specs/string.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const assert = require('assert');
2+
3+
require('../../iknowishouldnt.js');
4+
5+
describe('String.prototype.format', () => {
6+
7+
it('Testing string type.', () => {
8+
9+
assert.equal('Hello %s'.format('World!'), 'Hello World!');
10+
11+
});
12+
13+
it('Testing integer type with both flags.', () => {
14+
15+
assert.equal('hsl(%i, 50%, 50%)'.format(125.1), 'hsl(125, 50%, 50%)');
16+
assert.equal('hsl(%d, 50%, 50%)'.format(125.1), 'hsl(125, 50%, 50%)');
17+
18+
});
19+
20+
it('Testing float type.', () => {
21+
22+
assert.equal('font-size: %fem'.format(1.5), 'font-size: 1.5em');
23+
24+
});
25+
26+
it('Testing multiple types.', () => {
27+
28+
assert.equal('%s(%f, %i%, %i%, %f)'.format('hsla', 150.5, 50.0, 50.0, 0.5), 'hsla(150.5, 50%, 50%, 0.5)');
29+
30+
});
31+
32+
it('Testing string with no replacements.', () => {
33+
34+
assert.equal('Hello World!'.format('Test'), 'Hello World!');
35+
36+
});
37+
38+
});

0 commit comments

Comments
 (0)