Skip to content

Commit

Permalink
feat: string.capitalize method
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 31, 2022
1 parent b8ea4ab commit 32e7360
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ npm install ext
- [`random`](docs/string/random.md)
- `String.prototype`
- [`campelToHyphen`](docs/string_/camel-to-hyphen.md)
- [`capitalize`](docs/string_/capitalize.md)
- [`includes`](docs/string_/includes.md)
- `Thenable.prototype`
- [`finally`](docs/thenable_/finally.md)
8 changes: 0 additions & 8 deletions _es5-ext/string/#/capitalize.js

This file was deleted.

9 changes: 0 additions & 9 deletions _es5-ext/test/string/#/capitalize.js

This file was deleted.

9 changes: 9 additions & 0 deletions docs/string_/capitalize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `string.capitalize()` _(ext/string\_/capitalize)_

Capitalize input string, e.g. convert `this is a test` into `This is a test`.

```javascript
const capitalize = require("ext/string_/capitalize");

capitalize.call("this is a test"); // This is a test
```
9 changes: 9 additions & 0 deletions string_/capitalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var ensureString = require("type/string/ensure");

module.exports = function () {
var input = ensureString(this);
if (!input) return input;
return input.charAt(0).toUpperCase() + input.slice(1);
};
21 changes: 21 additions & 0 deletions test/string_/capitalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

var assert = require("chai").assert;

var capitalize = require("../../string_/capitalize");

describe("string_/capitalize", function () {
it("should capitalize lowercase word", function () {
assert.equal(capitalize.call("raz"), "Raz");
assert.equal(capitalize.call("raZ"), "RaZ");
});
it("should return as-is capitalized word", function () {
assert.equal(capitalize.call("Raz"), "Raz");
assert.equal(capitalize.call("RAZ"), "RAZ");
});
it("should return emptry string as is", function () { assert.equal(capitalize.call(""), ""); });
it("should capitalize single letter", function () { assert.equal(capitalize.call("a"), "A"); });
it("should capitalize sentence", function () {
assert.equal(capitalize.call("this is a Test"), "This is a Test");
});
});

0 comments on commit 32e7360

Please sign in to comment.