# base/node (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/node/

계층 객체.

## Node extends Object

계층 객체.
- 부모와 자식을 가질 수 있는 계층 구조 기능.
- 활성화 여부 지정 기능.

예제: 계층 만들고 이름으로 찾기

```js
import { Node } from "./build/vanilla.js";

const root = new Node();
root.setName("root");
const child = new Node();
child.setName("child");
root.addChild(child);
const grandChild = new Node();
grandChild.setName("grandChild");
child.addChild(grandChild);
child.setActive(false);
const found = root.findChildRecursiveByName("grandChild");
console.log(found === grandChild, grandChild.isActive(), grandChild.isActiveInHierarchy());
console.log(root.getChildCount(), grandChild.getParent() === child, root.isRoot(), grandChild.isLeaf());
```

- `constructor()` (constructor, since 0.0.8): 생성.
- `setParent(parent)` (method, since 0.0.8): 부모 설정.
  - `parent` Node | null: 새 부모. (null 이면 지금 부모에서 떼어 내기만 한다)
  - returns boolean: 붙였거나 떼었으면 참. (자신이나 자신의 자손 밑으로는 계층이 고리가 되므로 붙이지 않고 거짓)
- `addChild(child)` (method, since 0.0.8): 자식 추가.
  - `child` Node: 붙일 노드. (다른 부모에 붙어 있었으면 거기서 떼어 와 자식 목록 맨 뒤에 붙인다)
  - returns boolean: 붙였으면 참. (이 노드 자신이나 이 노드의 조상이면 붙이지 않고 거짓)
- `removeChild(child)` (method, since 0.0.8): 자식 제거.
  - `child` Node: 뗄 노드. (이 노드의 자식일 때만 떼어 낸다, 아니면 아무것도 하지 않는다)
- `removeChildAt(childIndex)` (method, since 0.0.11): 자식 제거.
  - `childIndex` number: 뗄 자식의 차례. (0 부터, 범위 밖이면 아무것도 하지 않는다)
- `removeChildren()` (method, since 0.0.8): 모든 자식 제거. (직계 자식 목록만 비우기 때문에 자식들이 소유한 계층 구조는 유지됨)
- `isRoot()` (method, since 0.0.8): 부모가 없는지 여부 반환.
  - returns boolean: 부모가 없으면 참.
- `isLeaf()` (method, since 0.0.8): 자식이 없는지 여부 반환.
  - returns boolean: 자식이 없으면 참.
- `hasParent()` (method, since 0.0.11): 부모 여부 반환.
  - returns boolean: 부모가 있으면 참.
- `getParent()` (method, since 0.0.8): 부모 반환.
  - returns Node: 부모 노드. (없으면 null)
- `getSiblings()` (method, since 0.0.11): 형제 목록 반환.
  - returns Node[] | null: 부모의 자식 목록. (자신도 들어 있는 원본 배열, 부모가 없으면 null)
- `getChildren()` (method, since 0.0.8): 자식 목록 반환.
  - returns Node[]: 직계 자식 목록. (복사하지 않은 원본 배열)
- `getChildCount()` (method, since 0.0.8): 자식 수 반환.
  - returns number: 직계 자식 수.
- `getChild(childIndex)` (method, since 0.0.8): 자식 반환.
  - `childIndex` number: 자식의 차례. (0 부터)
  - returns Node | null: 그 차례의 직계 자식. (범위 밖이면 null)
- `getChildIndex(child)` (method, since 0.0.11): 자식의 위치 반환.
  - `child` Node: 찾을 노드.
  - returns number: 직계 자식 목록에서의 차례. (자식이 아니면 -1)
- `hasChild(child)` (method, since 0.0.9): 자식 포함 여부 반환.
  - `child` Node: 찾을 노드.
  - returns boolean: 직계 자식이면 참.
- `setActive(active)` (method, since 0.0.8): 활성화 상태 설정.
  - `active` boolean: 참이면 활성. (자식의 값은 바꾸지 않는다)
- `isActive()` (method, since 0.0.8): 활성화 상태 반환.
  - returns boolean: 자신이 활성이면 참. (부모는 보지 않는다)
- `isActiveInHierarchy()` (method, since 0.0.8): 현재부터 루트까지 계층 전체의 활성화 상태 반환. (루트까지 하나라도 비활성화상태면 false 반환)
  - returns boolean: 자신과 루트까지의 모든 부모가 활성이면 참.
- `setName(name)` (method, since 0.0.11): 이름 설정.
  - `name` string: 노드 이름. (같은 이름을 막지 않는다)
- `getName()` (method, since 0.0.11): 이름 반환.
  - returns string: 노드 이름. (기본 빈 글)
- `findChild(predicate)` (method, since 0.0.11): 조건으로 직계 자식 찾기.
  - `predicate` function(Node): boolean: 직계 자식을 받아 고를 것이면 참을 돌려주는 함수. (null 이면 null 을 돌려준다)
  - returns Node | null: 자식 목록 차례로 처음 맞은 직계 자식. (없으면 null)
- `findChildRecursive(predicate)` (method, since 0.0.11): 조건으로 직계 자식 찾기. (깊이 우선)
  - `predicate` function(Node): boolean: 자손을 받아 고를 것이면 참을 돌려주는 함수.
  - returns Node | null: 깊이 우선으로 처음 맞은 자손. (손자 아래까지 모두 본다, 없으면 null)

  예제: 조건으로 하위 계층 전체에서 찾기

  ```js
  import { Node } from "./build/vanilla.js";
  
  const root = new Node();
  const child = new Node();
  const grandChild = new Node();
  root.addChild(child);
  child.addChild(grandChild);
  grandChild.setActive(false);
  const inactiveNode = root.findChildRecursive((node) => {
  	return !node.isActive();
  });
  console.log(inactiveNode === grandChild);
  ```

- `findChildByName(name)` (method, since 0.0.11): 이름으로 직계 자식 찾기.
  - `name` string: 찾을 이름.
  - returns Node | null: 이름이 같은 첫 직계 자식. (없으면 null)
- `findChildRecursiveByName(name)` (method, since 0.0.11): 이름으로 하위 계층 전체에서 찾기. (깊이 우선)
  - `name` string: 찾을 이름.
  - returns Node | null: 깊이 우선으로 처음 맞은, 이름이 같은 자손. (없으면 null)
- `findDescendantByName(name)` (method, since 0.4.0): 이름으로 하위 계층 전체에서 찾기. (깊이 우선, findChildRecursiveByName 과 같다, 러스트 판 find_descendant_by_name 과 같은 이름)
  - `name` string: 찾을 이름.
  - returns Node | null: 깊이 우선으로 처음 맞은, 이름이 같은 자손. (없으면 null)
- `destroyChildrenWhere(predicate)` (method, since 0.4.0): 조건에 맞는 직계 자식 떼어 파괴. (떼어 낸 수 반환, 러스트 판 destroy_children_where 와 같은 이름)
  - `predicate` function(Node): boolean: 직계 자식을 받아 떼어 파괴할 것이면 참을 돌려주는 함수.
  - returns number: 떼어 파괴한 자식 수.

  예제: 조건에 맞는 자식 떼어 파괴하기

  ```js
  import { Node } from "./build/vanilla.js";
  
  const root = new Node();
  for (let index = 0; index < 4; ++index) {
  	const child = new Node();
  	child.setName("enemy" + index);
  	child.setActive(index % 2 === 0);
  	root.addChild(child);
  }
  const destroyedCount = root.destroyChildrenWhere((child) => {
  	return !child.isActive();
  });
  console.log(destroyedCount, root.getChildCount());
  ```

- `removeChildByName(name)` (method, since 0.0.11): 이름으로 직계 자식 제거.
  - `name` string: 뗄 직계 자식의 이름. (같은 이름이 여럿이면 첫 자식만 뗀다)
- `setSiblingIndex(childIndex)` (method, since 0.0.11): 형제간의 위치 설정.
  - `childIndex` number: 형제 사이의 새 차례. (0 ~ 형제 수 - 1 로 자른다, 부모가 없으면 아무것도 하지 않는다)

  예제: 형제 사이에서 맨 앞, 맨 뒤로 옮기기

  ```js
  import { Node } from "./build/vanilla.js";
  
  const root = new Node();
  const first = new Node();
  const second = new Node();
  const third = new Node();
  root.addChild(first);
  root.addChild(second);
  root.addChild(third);
  third.setSiblingIndex(0);
  first.setSiblingIndex(100);
  console.log(third.getSiblingIndex(), second.getSiblingIndex(), first.getSiblingIndex());
  ```

- `getSiblingIndex()` (method, since 0.0.11): 형제간의 위치 반환.
  - returns number: 형제 사이의 차례. (부모가 없으면 -1)
