LoginSignup
8
8

More than 5 years have passed since last update.

for...of を使って基本的な文字列メソッドを定義する

Last updated at Posted at 2015-01-17

2015年1月にリリースされた io.js では for...of を使うことができる。for...of はサロゲートペアを考慮する。String.prototype.codePointAt() よりもコードがコンパクトになる。codePointAt のコードの例はこちらの記事を参照。

文字数を求める

if (!String.prototype.countChar32) {
    String.prototype.countChar32 = function() {
        var count = 0;

        for (var c of this) {
            ++count;
        }

        return count;
    };
}

var str = '\uD842\uDFB7野家';
console.log(3 === str.countChar32());

指定したインデックスに対応する1文字取り出す

if (!String.prototype.char32At) {
    String.prototype.char32At = function(offset) {

        var index = 0;

        for (var c of this) {

            if (offset === index) {
                return c;
            }

            ++index;
        }

        return '';
    };
}

var str = '\uD842\uDFB7野家';
console.log('\uD842\uDFB7' === str.char32At(0));

文字列を逆順に並べ替える

if (!String.prototype.reverse) {
    String.prototype.reverse = function() {

        var ret = '';

        for (var c of this) {
            ret = c + ret;
        }

        return ret;
    };
}

var str = '\uD842\uDFB7野家';
console.log('家野\uD842\uDFB7' === str.reverse());

部分文字列を取り出す

if (!String.prototype.substr32) {
    String.prototype.substr32 = function(pos, length) {

        var ret = '';
        var last = pos + length - 1;
        var index = 0;

        for (var c of this) {

            if (index >= pos && last >= index) {
                ret += c;
            }

            if (index === last) {
                return ret;
            }

            ++index;
        }

        return ret;
    };
}

var str = '\uD842\uDFB7野家';
console.log('' === str.substr32(1, 1));

部分文字列を取り出す・その2

関数型言語に見られる takedrop を定義する。

if (!String.prototype.take) {
    String.prototype.take = function(length) {

        var ret = '';
        var index = 0;

        for (var c of this) {

            if (index === length) {
                return ret;
            }

            ret += c;
            ++index;
        }

        return ret;
    };
}

var str = '\uD842\uDFB7野家';
console.log('\uD842\uDFB7' === str.take(1));
if (!String.prototype.drop) {
    String.prototype.drop = function(length) {

        var ret = '';
        var index = 0;

        for (var c of this) {
            ++index;

            if (index <= length) {
                continue;
            }

            ret += c;
        }

        return ret;
    };
}

var str = '\uD842\uDFB7野家';
console.log('野家' === str.drop(1));

1文字ごとにコールバックを適用する

if (!String.prototype.eachChar) {
    String.prototype.eachChar = function(callback) {

        for (var c of this) {
            callback(c);
        }

    };
}

var str = '\uD842\uDFB7野家';
str.eachChar(function(c) {
    console.log(c);
});

コードポイントごとにコールバックを適用する

if (!String.prototype.eachCodePoint) {
    String.prototype.eachCodePoint = function(callback) {

        for (var c of this) {
            callback(c.codePointAt(0));
        }

    };
}

var str = '\uD842\uDFB7野家';
str.eachCodePoint(function(cp) {
    console.log('U+' + cp.toString(16).toUpperCase());
});
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8