preface

The size function returns the length of an array, the length of an array-like object, a Set and a Map, the number of keys, and the length of a string

Thought analysis

Source code analysis

1. size

1. Pass in parameters
  • collectionPassing in objects, class arrays, arrays,set,map,stringequivalent
2. Source code analysis
function size(collection) {
  if (collection == null) {
    return 0;
  }
  if (isArrayLike(collection)) {
    return isString(collection) ? stringSize(collection) : collection.length;
  }
  var tag = getTag(collection);
  if (tag == mapTag || tag == setTag) {
    return collection.size;
  }
  return baseKeys(collection).length;
}
Copy the code

Check whether it is null

if (collection == null) {
    return 0;
}
Copy the code

Check whether it is an Array of classes, including String and Array

if (isArrayLike(collection)) {
    return isString(collection) ? stringSize(collection) : collection.length;
}
Copy the code

Determine whether it is a map or a set using the size attribute of both types

var tag = getTag(collection);
if (tag == mapTag || tag == setTag) {
    return collection.size;
}
Copy the code

Returns the key of the object

return baseKeys(collection).length;
Copy the code

2. stringSize

1. Pass in parameters

• string A common character string

2. Source code analysis

The stringSize function distinguishes strings that contain special Unicode, using the unicodeSize method alone to determine the stringSize

function stringSize(string) {
  return hasUnicode(string)
    ? unicodeSize(string)
  : asciiSize(string);
}
Copy the code

3. unicodeSize

1. Pass in parameters

• String A string containing special Unicode characters

2. Source code analysis

/[\u200d\ UD800 -\udfff\u0300-\u036f\ufe20-\ufe2f\u20d0-\u20ff\ufe0e\ufe0f]/ The main function of unicodeSize is to correctly output the length of the string combined with the hyphen. The main function of unicodeSize is to output the length of the string combined with the hyphen

function unicodeSize(string) {
  var result = reUnicode.lastIndex = 0;
  while (reUnicode.test(string)) {
    ++result;
  }
  return result;
}
Copy the code

4. baseKeys

1. Pass in parameters

• Object The object is passed in

2. Source code analysis
function baseKeys(object) {
  if(! isPrototype(object)) {return nativeKeys(object);
  }
  var result = [];
  for (var key in Object(object)) {
    if(hasOwnProperty.call(object, key) && key ! ='constructor') { result.push(key); }}return result;
}
Copy the code

Keys retrieves the key array of an Object with no archetype and returns it

  if(! isPrototype(object)) {return nativeKeys(object);
  }
Copy the code

HasOwnProperty retrieves the key array of the object with the stereotype and returns it

  var result = [];
  for (var key in Object(object)) {
    if(hasOwnProperty.call(object, key) && key ! ='constructor') { result.push(key); }}return result;
Copy the code

Application scenarios

The normal size function is certainly used to determine the size of an array, a class array, a string, etc. In particular, it can be used to print the size of a string according to the Unicode encoding. For example, in the following string, \u0300 and \u200d are two special Unicode characters. They are the combination of restraining notes (shown as pinyin four-tone symbols) and zero-width connectors. These characters will be combined when they are connected together. When size is used to measure the size, it will be shown as the combined size, and when length is measured, it will be shown as the number of characters

'\u0300a\u200d\u200da'.length  / / 5
_.size('\u0300a\u200d\u200da') / / 3
'a\u0300a'.length / / 3
_.size('a\u0300a') / / 2
Copy the code

conclusion

The size function is a relatively simple function, but it also has some clever ideas, most notably the ability to determine the length of a string based on the Unicode character meaning