UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

34 lines (33 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.strtok = strtok; const _phpRuntimeState_ts_1 = require("../_helpers/_phpRuntimeState.js"); function strtok(str, tokens) { // discuss at: https://locutus.io/php/strtok/ // original by: Brett Zamir (https://brett-zamir.me) // note 1: Use tab and newline as tokenizing characters as well // example 1: var $string = "\t\t\t\nThis is\tan example\nstring\n" // example 1: var $tok = strtok($string, " \n\t") // example 1: var $b = '' // example 1: while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");} // example 1: var $result = $b // returns 1: "Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n" if (typeof tokens === 'undefined') { tokens = str; str = (0, _phpRuntimeState_ts_1.getPhpRuntimeString)('strtokleftOver', ''); } if (str.length === 0) { return false; } if (tokens.includes(str.charAt(0))) { return strtok(str.substring(1), tokens); } let i = 0; for (; i < str.length; i++) { if (tokens.includes(str.charAt(i))) { break; } } (0, _phpRuntimeState_ts_1.setPhpRuntimeEntry)('strtokleftOver', str.substring(i + 1)); return str.substring(0, i); }